Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide UFCS-style completion on content types #849

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/tinymist-query/src/analysis/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::syntax::{
scan_workspace_files, Decl, DefKind, DerefTarget, ExprInfo, ExprRoute, LexicalScope,
ModuleDependency,
};
use crate::upstream::{tooltip_, Tooltip};
use crate::upstream::{tooltip_, CompletionFeat, Tooltip};
use crate::{
lsp_to_typst, typst_to_lsp, ColorTheme, CompilerQueryRequest, LspPosition, LspRange,
LspWorldExt, PositionEncoding, TypstRange, VersionedDocument,
Expand All @@ -55,6 +55,8 @@ pub struct Analysis {
pub allow_multiline_token: bool,
/// Whether to remove html from markup content in responses.
pub remove_html: bool,
/// Tinymist's completion features.
pub completion_feat: CompletionFeat,
/// The editor's color theme.
pub color_theme: ColorTheme,
/// The periscope provider.
Expand Down
1 change: 1 addition & 0 deletions crates/tinymist-query/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ impl StatefulRequest for CompletionRequest {
}
}),
text_edit: Some(text_edit),
additional_text_edits: typst_completion.additional_text_edits.clone(),
insert_text_format: Some(InsertTextFormat::SNIPPET),
commit_characters: typst_completion
.commit_char
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/func_args.typ
"labelDetails": {
"description": "type"
},
"sortText": "053",
"sortText": "052",
"textEdit": {
"newText": "content",
"range": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/func_builtin_args.typ
"labelDetails": {
"description": "(int, content, gutter: relative) => columns"
},
"sortText": "057",
"sortText": "056",
"textEdit": {
"newText": "columns(${1:})",
"range": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ input_file: crates/tinymist-query/src/fixtures/completion/func_with_args.typ
"labelDetails": {
"description": "type"
},
"sortText": "053",
"sortText": "052",
"textEdit": {
"newText": "content",
"range": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ input_file: crates/tinymist-query/src/fixtures/pkgs/touying-core-slides.typ
"labelDetails": {
"description": "() => any"
},
"sortText": "051",
"sortText": "050",
"textEdit": {
"newText": "config-xxx()${1:}",
"range": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ input_file: crates/tinymist-query/src/fixtures/pkgs/touying-core-slides.typ
"labelDetails": {
"description": "(content, gap: length, justify: bool) => repeat"
},
"sortText": "250",
"sortText": "249",
"textEdit": {
"newText": "repeat[${1:}]",
"range": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ input_file: crates/tinymist-query/src/fixtures/pkgs/touying-utils-cover-with-rec
"labelDetails": {
"description": "type"
},
"sortText": "296",
"sortText": "295",
"textEdit": {
"newText": "stroke(${1:})",
"range": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ input_file: crates/tinymist-query/src/fixtures/pkgs/touying-utils-current-headin
"labelDetails": {
"description": "type"
},
"sortText": "134",
"sortText": "133",
"textEdit": {
"newText": "int(${1:})",
"range": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ input_file: crates/tinymist-query/src/fixtures/pkgs/touying-utils-markup-text.ty
"labelDetails": {
"description": "type"
},
"sortText": "288",
"sortText": "287",
"textEdit": {
"newText": "str(${1:})",
"range": {
Expand Down
2 changes: 1 addition & 1 deletion crates/tinymist-query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod ty;
mod upstream;

pub use analysis::{LocalContext, LocalContextGuard, LspWorldExt};
pub use upstream::with_vm;
pub use upstream::{with_vm, CompletionFeat};

mod diagnostics;
pub use diagnostics::*;
Expand Down
25 changes: 19 additions & 6 deletions crates/tinymist-query/src/upstream/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::ops::Range;

use ecow::{eco_format, EcoString};
use if_chain::if_chain;
use lsp_types::TextEdit;
use serde::{Deserialize, Serialize};
use typst::foundations::{fields_on, format_str, repr, Repr, StyleChain, Styles, Value};
use typst::model::Document;
Expand All @@ -15,12 +16,11 @@ use unscanny::Scanner;

use super::{plain_docs_sentence, summarize_font_family};
use crate::adt::interner::Interned;
use crate::analysis::{analyze_labels, DynLabel, Ty};
use crate::LocalContext;
use crate::analysis::{analyze_labels, DynLabel, LocalContext, Ty};

mod ext;
pub use ext::complete_path;
use ext::*;
pub use ext::{complete_path, CompletionFeat};

/// Autocomplete a cursor position in a source file.
///
Expand Down Expand Up @@ -73,6 +73,10 @@ pub struct Completion {
pub apply: Option<EcoString>,
/// An optional short description, at most one sentence.
pub detail: Option<EcoString>,
/// An optional array of additional text edits that are applied when
/// selecting this completion. Edits must not overlap with the main edit
/// nor with themselves.
pub additional_text_edits: Option<Vec<TextEdit>>,
/// An optional command to run when the completion is selected.
pub command: Option<&'static str>,
}
Expand Down Expand Up @@ -382,7 +386,7 @@ fn complete_field_accesses(ctx: &mut CompletionContext) -> bool {
if let Some((value, styles)) = ctx.ctx.analyze_expr(&prev).into_iter().next();
then {
ctx.from = ctx.cursor;
field_access_completions(ctx, &value, &styles);
field_access_completions(ctx, &prev, &value, &styles);
return true;
}
}
Expand All @@ -397,7 +401,7 @@ fn complete_field_accesses(ctx: &mut CompletionContext) -> bool {
if let Some((value, styles)) = ctx.ctx.analyze_expr(&prev_prev).into_iter().next();
then {
ctx.from = ctx.leaf.offset();
field_access_completions(ctx, &value, &styles);
field_access_completions(ctx,&prev_prev, &value, &styles);
return true;
}
}
Expand All @@ -406,7 +410,12 @@ fn complete_field_accesses(ctx: &mut CompletionContext) -> bool {
}

/// Add completions for all fields on a value.
fn field_access_completions(ctx: &mut CompletionContext, value: &Value, styles: &Option<Styles>) {
fn field_access_completions(
ctx: &mut CompletionContext,
node: &LinkedNode,
value: &Value,
styles: &Option<Styles>,
) {
for (name, value, _) in value.ty().scope().iter() {
ctx.value_completion(Some(name.clone()), value, true, None);
}
Expand Down Expand Up @@ -443,11 +452,15 @@ fn field_access_completions(ctx: &mut CompletionContext, value: &Value, styles:
});
}
}

ctx.ufcs_completions(node, value);
}
Value::Content(content) => {
for (name, value) in content.fields() {
ctx.value_completion(Some(name.into()), &value, false, None);
}

ctx.ufcs_completions(node, value);
}
Value::Dict(dict) => {
for (name, value) in dict.iter() {
Expand Down
Loading
Loading