Skip to content

Commit 38974a3

Browse files
feat: autofix unknown variable: a and file not found (searched at a.typ) by code action (#1743)
* feat: check context * feat: implement it * fix: warnings * test: update snapshot
1 parent d6d3766 commit 38974a3

26 files changed

+834
-84
lines changed

crates/tinymist-query/src/analysis/code_action.rs

Lines changed: 285 additions & 57 deletions
Large diffs are not rendered by default.

crates/tinymist-query/src/analysis/completion.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,11 @@ type Cursor<'a> = CompletionCursor<'a>;
377377

378378
/// A node selected by [`CompletionCursor`].
379379
enum SelectedNode<'a> {
380-
/// Selects an identifier, e.g. `foo|` or `fo|o`.
380+
/// Selects an identifier, e.g. `foobar|` or `foo|bar`.
381381
Ident(LinkedNode<'a>),
382-
/// Selects a label, e.g. `<foo|>` or `<fo|o>`.
382+
/// Selects a label, e.g. `<foobar|>` or `<foo|bar>`.
383383
Label(LinkedNode<'a>),
384-
/// Selects a reference, e.g. `@foo|` or `@fo|o`.
384+
/// Selects a reference, e.g. `@foobar|` or `@foo|bar`.
385385
Ref(LinkedNode<'a>),
386386
}
387387

crates/tinymist-query/src/analysis/global.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ pub struct Analysis {
6565
pub allow_multiline_token: bool,
6666
/// Whether to remove html from markup content in responses.
6767
pub remove_html: bool,
68+
/// Whether to utilize the extended `tinymist.resolveCodeAction` at client
69+
/// side.
70+
///
71+
/// The extended feature by `tinymist.resolveCodeAction`:
72+
/// - supports Snippet edit.
73+
///
74+
/// The example implementation can be found in the VS Code extension.
75+
pub extended_code_action: bool,
6876
/// Tinymist's completion features.
6977
pub completion_feat: CompletionFeat,
7078
/// The editor's color theme.

crates/tinymist-query/src/analysis/link_expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ impl LinkTarget {
6060
LinkTarget::Url(url) => Some(url.as_ref().clone()),
6161
LinkTarget::Path(id, path) => {
6262
// Avoid creating new ids here.
63-
let root = ctx.path_for_id(id.join("")).ok()?;
64-
crate::path_res_to_url(root.join(path).ok()?).ok()
63+
let root = ctx.path_for_id(id.join("/")).ok()?;
64+
let path_in_workspace = id.vpath().join(Path::new(path.as_str()));
65+
let path = root.resolve_to(&path_in_workspace)?;
66+
crate::path_res_to_url(path).ok()
6567
}
6668
}
6769
}

crates/tinymist-query/src/code_action.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
use lsp_types::CodeActionContext;
2+
13
use crate::{analysis::CodeActionWorker, prelude::*, SemanticRequest};
24

5+
pub(crate) mod proto;
6+
37
/// The [`textDocument/codeAction`] request is sent from the client to the
48
/// server to compute commands for a given text document and range. These
59
/// commands are typically code fixes to either fix problems or to
@@ -64,18 +68,23 @@ pub struct CodeActionRequest {
6468
pub path: PathBuf,
6569
/// The range of the document to get code actions for.
6670
pub range: LspRange,
71+
/// The context of the code action request.
72+
pub context: CodeActionContext,
6773
}
6874

6975
impl SemanticRequest for CodeActionRequest {
70-
type Response = Vec<CodeActionOrCommand>;
76+
type Response = Vec<CodeAction>;
7177

7278
fn request(self, ctx: &mut LocalContext) -> Option<Self::Response> {
79+
log::info!("requested code action: {self:?}");
80+
7381
let source = ctx.source_by_path(&self.path).ok()?;
7482
let range = ctx.to_typst_range(self.range, &source)?;
7583

7684
let root = LinkedNode::new(source.root());
7785
let mut worker = CodeActionWorker::new(ctx, source.clone());
78-
worker.work(root, range);
86+
worker.autofix(&root, &range, &self.context);
87+
worker.scoped(&root, &range);
7988

8089
(!worker.actions.is_empty()).then_some(worker.actions)
8190
}
@@ -94,12 +103,13 @@ mod tests {
94103
let request = CodeActionRequest {
95104
path: path.clone(),
96105
range: find_test_range(&source),
106+
context: CodeActionContext::default(),
97107
};
98108

99109
let result = request.request(ctx);
100110

101111
with_settings!({
102-
description => format!("Code Action on {}", make_range_annoation(&source)),
112+
description => format!("Code Action on {}", make_range_annotation(&source)),
103113
}, {
104114
assert_snapshot!(JsonRepr::new_redacted(result, &REDACT_LOC));
105115
})

0 commit comments

Comments
 (0)