feat: prefer to select the previous token when cursor is before a marker (#1175)

* feat: prefer to select the previous token when cursor is before a marker

* test: update snapshot
This commit is contained in:
Myriad-Dreamin 2025-01-15 14:33:26 +08:00 committed by GitHub
parent 0aafee1d13
commit faf807edb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 26 additions and 13 deletions

View file

@ -30,8 +30,9 @@ use crate::analysis::{
};
use crate::docs::{DefDocs, TidyModuleDocs};
use crate::syntax::{
classify_syntax, construct_module_dependencies, resolve_id_by_path, scan_workspace_files, Decl,
DefKind, ExprInfo, ExprRoute, LexicalScope, ModuleDependency, SyntaxClass,
classify_syntax, construct_module_dependencies, is_mark, resolve_id_by_path,
scan_workspace_files, Decl, DefKind, ExprInfo, ExprRoute, LexicalScope, ModuleDependency,
SyntaxClass,
};
use crate::upstream::{tooltip_, Tooltip};
use crate::{
@ -622,15 +623,27 @@ impl SharedContext {
}
/// Classifies the syntax under position that can be operated on by IDE
/// functionality.
pub fn classify_pos<'s>(
/// functionality. It is preferred to select a decl if it is at the starts
/// of some mark.
pub fn classify_for_decl<'s>(
&self,
source: &'s Source,
position: LspPosition,
shift: usize,
) -> Option<SyntaxClass<'s>> {
let cursor = self.to_typst_pos_offset(source, position, shift)?;
let node = LinkedNode::new(source.root()).leaf_at_compat(cursor)?;
let cursor = self.to_typst_pos_offset(source, position, 1)?;
let mut node = LinkedNode::new(source.root()).leaf_at_compat(cursor)?;
// In the case that the cursor is at the end of an identifier.
// e.g. `f(x|)`, we will select the `x`
if cursor == node.offset() + 1 && is_mark(node.kind()) {
let prev_leaf = node.prev_leaf();
if let Some(prev_leaf) = prev_leaf {
if prev_leaf.range().end == node.offset() {
node = prev_leaf;
}
}
}
classify_syntax(node, cursor)
}

View file

@ -32,7 +32,7 @@ impl StatefulRequest for GotoDefinitionRequest {
doc: Option<VersionedDocument>,
) -> Option<Self::Response> {
let source = ctx.source_by_path(&self.path).ok()?;
let syntax = ctx.classify_pos(&source, self.position, 1)?;
let syntax = ctx.classify_for_decl(&source, self.position)?;
let origin_selection_range = ctx.to_lsp_range(syntax.node().range(), &source);
let def = ctx.def_of_syntax(&source, doc.as_ref(), syntax)?;

View file

@ -38,7 +38,7 @@ impl StatefulRequest for PrepareRenameRequest {
doc: Option<VersionedDocument>,
) -> Option<Self::Response> {
let source = ctx.source_by_path(&self.path).ok()?;
let syntax = ctx.classify_pos(&source, self.position, 1)?;
let syntax = ctx.classify_for_decl(&source, self.position)?;
if matches!(syntax.node().kind(), SyntaxKind::FieldAccess) {
// todo: rename field access
log::info!("prepare_rename: field access is not a definition site");

View file

@ -31,7 +31,7 @@ impl StatefulRequest for ReferencesRequest {
doc: Option<VersionedDocument>,
) -> Option<Self::Response> {
let source = ctx.source_by_path(&self.path).ok()?;
let syntax = ctx.classify_pos(&source, self.position, 1)?;
let syntax = ctx.classify_for_decl(&source, self.position)?;
let locations = find_references(ctx, &source, doc.as_ref(), syntax)?;

View file

@ -42,7 +42,7 @@ impl StatefulRequest for RenameRequest {
doc: Option<VersionedDocument>,
) -> Option<Self::Response> {
let source = ctx.source_by_path(&self.path).ok()?;
let syntax = ctx.classify_pos(&source, self.position, 1)?;
let syntax = ctx.classify_for_decl(&source, self.position)?;
let def = ctx.def_of_syntax(&source, doc.as_ref(), syntax.clone())?;

View file

@ -374,7 +374,7 @@ fn e2e() {
});
let hash = replay_log(&tinymist_binary, &root.join("neovim"));
insta::assert_snapshot!(hash, @"siphash128_13:13c8f3b8332d33ca70a079e4a96cc55f");
insta::assert_snapshot!(hash, @"siphash128_13:cd193b47cc7db3edf339eb048f34f4e2");
}
{
@ -385,7 +385,7 @@ fn e2e() {
});
let hash = replay_log(&tinymist_binary, &root.join("vscode"));
insta::assert_snapshot!(hash, @"siphash128_13:1d6bc2aac362b883a197487869cddf0e");
insta::assert_snapshot!(hash, @"siphash128_13:e9f262b45e59ec0c1f282f36103c419");
}
}