Quellcode durchsuchen

Shell: Make Juxtaposition autocompletion smarter

Now something like `"$HOME"/` autocompletes correctly.
Note that only the first element of lists is used to autocomplete
things.
Ali Mohammad Pur vor 3 Jahren
Ursprung
Commit
78d1093dab
1 geänderte Dateien mit 22 neuen und 9 gelöschten Zeilen
  1. 22 9
      Userland/Shell/AST.cpp

+ 22 - 9
Userland/Shell/AST.cpp

@@ -3013,23 +3013,36 @@ void Juxtaposition::highlight_in_editor(Line::Editor& editor, Shell& shell, High
 Vector<Line::CompletionSuggestion> Juxtaposition::complete_for_editor(Shell& shell, size_t offset, const HitTestResult& hit_test_result)
 {
     auto matching_node = hit_test_result.matching_node;
+    if (m_left->would_execute() || m_right->would_execute()) {
+        return {};
+    }
+
     // '~/foo/bar' is special, we have to actually resolve the tilde
     // then complete the bareword with that path prefix.
-    if (m_right->is_bareword() && m_left->is_tilde()) {
-        auto tilde_value = m_left->run(shell)->resolve_as_list(shell)[0];
+    auto left_values = m_left->run(shell)->resolve_as_list(shell);
 
-        auto corrected_offset = offset - matching_node->position().start_offset;
-        auto* node = static_cast<BarewordLiteral*>(matching_node.ptr());
+    if (left_values.is_empty())
+        return m_right->complete_for_editor(shell, offset, hit_test_result);
 
-        if (corrected_offset > node->text().length())
-            return {};
+    auto& left_value = left_values.first();
 
-        auto text = node->text().substring(1, node->text().length() - 1);
+    auto right_values = m_right->run(shell)->resolve_as_list(shell);
+    StringView right_value {};
 
-        return shell.complete_path(tilde_value, text, corrected_offset - 1, Shell::ExecutableOnly::No);
+    auto corrected_offset = offset - matching_node->position().start_offset;
+
+    if (!right_values.is_empty())
+        right_value = right_values.first();
+
+    if (m_left->is_tilde() && !right_value.is_empty()) {
+        right_value = right_value.substring_view(1);
+        corrected_offset--;
     }
 
-    return Node::complete_for_editor(shell, offset, hit_test_result);
+    if (corrected_offset > right_value.length())
+        return {};
+
+    return shell.complete_path(left_value, right_value, corrected_offset, Shell::ExecutableOnly::No);
 }
 
 HitTestResult Juxtaposition::hit_test_position(size_t offset) const