diff --git a/Shell/AST.cpp b/Shell/AST.cpp index 5754eb3463..b3bb5b71c1 100644 --- a/Shell/AST.cpp +++ b/Shell/AST.cpp @@ -807,7 +807,35 @@ HitTestResult FunctionDeclaration::hit_test_position(size_t offset) if (!position().contains(offset)) return {}; - return m_block->hit_test_position(offset); + auto result = m_block->hit_test_position(offset); + if (result.matching_node && result.matching_node->is_simple_variable()) + result.closest_node_with_semantic_meaning = this; + return result; +} + +Vector FunctionDeclaration::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result) +{ + auto matching_node = hit_test_result.matching_node; + if (!matching_node) + return {}; + + if (!matching_node->is_simple_variable()) + return matching_node->complete_for_editor(shell, offset, hit_test_result); + + auto corrected_offset = offset - matching_node->position().start_offset - 1; // Skip the first '$' + auto* node = static_cast(matching_node.ptr()); + + auto name = node->name().substring_view(0, corrected_offset); + + Vector results; + for (auto& arg : m_arguments) { + if (arg.name.starts_with(name)) + results.append(arg.name); + } + + results.append(matching_node->complete_for_editor(shell, offset, hit_test_result)); + + return results; } FunctionDeclaration::FunctionDeclaration(Position position, NameWithPosition name, Vector arguments, RefPtr body) diff --git a/Shell/AST.h b/Shell/AST.h index 0d76a9dbbf..013d24e67a 100644 --- a/Shell/AST.h +++ b/Shell/AST.h @@ -392,6 +392,7 @@ public: virtual bool is_glob() const { return false; } virtual bool is_tilde() const { return false; } virtual bool is_variable_decls() const { return false; } + virtual bool is_simple_variable() const { return false; } virtual bool is_syntax_error() const { return m_is_syntax_error; } virtual bool is_list() const { return false; } @@ -650,6 +651,7 @@ private: virtual RefPtr run(RefPtr) override; virtual void highlight_in_editor(Line::Editor&, Shell&, HighlightMetadata = {}) override; virtual HitTestResult hit_test_position(size_t) override; + virtual Vector complete_for_editor(Shell&, size_t, const HitTestResult&) override; virtual String class_name() const override { return "FunctionDeclaration"; } virtual bool would_execute() const override { return true; } @@ -849,6 +851,8 @@ public: SimpleVariable(Position, String); virtual ~SimpleVariable(); + const String& name() const { return m_name; } + private: virtual void dump(int level) const override; virtual RefPtr run(RefPtr) override; @@ -856,6 +860,7 @@ private: virtual Vector complete_for_editor(Shell&, size_t, const HitTestResult&) override; virtual HitTestResult hit_test_position(size_t) override; virtual String class_name() const override { return "SimpleVariable"; } + virtual bool is_simple_variable() const override { return true; } String m_name; };