editor: Fix function completion expansion in string contexts and call expressions (#30351)
Smit Barmase
created
Closes #27582
Now, when accepting function completion, it doesn't expand with
parentheses and arguments in the following cases:
1. If it's in a string (like `type Foo = MyClass["sayHello"]` instead of
`type Foo = MyClass["sayHello(name)"]`)
2. If it's in a call expression (like `useRef<HTMLDivElement>(null)`
over `useRef(initialValue)<HTMLDivElement>(null)`)
This is a follow-up to https://github.com/zed-industries/zed/pull/30312,
more like cleaner version of it.
Release Notes:
- Fixed an issue where accepting a method as an object string in
JavaScript would incorrectly expand. E.g. `MyClass["sayHello(name)"]`
instead of `MyClass["sayHello"]`.
@@ -828,6 +828,8 @@ pub struct LanguageConfigOverride {
pub completion_query_characters: Override<HashSet<char>>,
#[serde(default)]
pub opt_into_language_servers: Vec<LanguageServerName>,
+ #[serde(default)]
+ pub prefer_label_for_snippet: Option<bool>,
}
#[derive(Clone, Deserialize, Debug, Serialize, JsonSchema)]
@@ -1788,6 +1790,18 @@ impl LanguageScope {
)
}
+ /// Returns whether to prefer snippet `label` over `new_text` to replace text when
+ /// completion is accepted.
+ ///
+ /// In cases like when cursor is in string or renaming existing function,
+ /// you don't want to expand function signature instead just want function name
+ /// to replace existing one.
+ pub fn prefers_label_for_snippet_in_completion(&self) -> bool {
+ self.config_override()
+ .and_then(|o| o.prefer_label_for_snippet)
+ .unwrap_or(false)
+ }
+
/// Returns a list of bracket pairs for a given language with an additional
/// piece of information about whether the particular bracket pair is currently active for a given language.
pub fn brackets(&self) -> impl Iterator<Item = (&BracketPair, bool)> {