summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authordavidot <davidot@serenityos.org>2021-10-03 13:20:15 +0200
committerAndreas Kling <kling@serenityos.org>2021-10-03 17:42:05 +0200
commitf4f139773557d6254966617c45c5c34291d3aed9 (patch)
treeabaf825c029e594142ad6b0dbe7ad940010fa6d0 /Userland
parent0be0e7ea6efada35ba6eab72041009501e192892 (diff)
downloadserenity-f4f139773557d6254966617c45c5c34291d3aed9.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp9
-rw-r--r--Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.h3
-rw-r--r--Userland/Utilities/js.cpp6
3 files changed, 18 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/DeclarativeEnvironment.cpp
index 5f56b2ecb9..c90a08653b 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<ScopeNode>,
set_mutable_binding(global_object, name, value, false);
}
+Vector<String> DeclarativeEnvironment::bindings() const
+{
+ Vector<String> 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 07e7775399..8c5b759e6b 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<ScopeNode>, 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<String> bindings() const;
+
protected:
virtual void visit_edges(Visitor&) override;
diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp
index 6148243228..9f4b900e85 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;