From f4f139773557d6254966617c45c5c34291d3aed9 Mon Sep 17 00:00:00 2001 From: davidot Date: Sun, 3 Oct 2021 13:20:15 +0200 Subject: [PATCH] js: Allow for completion of lexically declared variables This does require us to have a method which lists all the bindings in a declarative environment which is not in the spec. --- .../Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp | 9 +++++++++ .../Libraries/LibJS/Runtime/DeclarativeEnvironment.h | 3 +++ Userland/Utilities/js.cpp | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp index 5f56b2ecb9a..c90a08653be 100644 --- a/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp +++ b/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp @@ -142,4 +142,13 @@ void DeclarativeEnvironment::initialize_or_set_mutable_binding(Badge, set_mutable_binding(global_object, name, value, false); } +Vector DeclarativeEnvironment::bindings() const +{ + Vector names; + for (auto& binding : m_bindings) { + names.empend(binding.key); + } + return names; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.h b/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.h index 07e7775399f..8c5b759e6ba 100644 --- a/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.h +++ b/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.h @@ -31,6 +31,9 @@ public: void initialize_or_set_mutable_binding(Badge, GlobalObject& global_object, FlyString const& name, Value value); + // This is not a method defined in the spec! Do not use this in any LibJS (or other spec related) code. + [[nodiscard]] Vector bindings() const; + protected: virtual void visit_edges(Visitor&) override; diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index 6148243228e..9f4b900e85e 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -1361,6 +1361,12 @@ int main(int argc, char** argv) case CompleteVariable: { auto const& variable = interpreter->global_object(); list_all_properties(variable.shape(), variable_name); + + for (String& name : global_environment.declarative_record().bindings()) { + if (name.starts_with(variable_name)) + results.empend(name); + } + if (results.size()) editor.suggest(variable_name.length()); break;