feat: complete text.lang/region (#199)

This commit is contained in:
Myriad-Dreamin 2024-04-19 16:12:40 +08:00 committed by GitHub
parent 0982686258
commit f6f2454d37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 148 additions and 4 deletions

View file

@ -117,8 +117,10 @@ pub(in crate::analysis::ty) fn param_mapping(f: &Func, p: &ParamInfo) -> Option<
PathPreference::Bibliography,
))),
("text", "size") => Some(FlowType::Builtin(FlowBuiltinType::TextSize)),
("text" | "stack", "dir") => Some(FlowType::Builtin(FlowBuiltinType::Dir)),
("text", "font") => Some(FlowType::Builtin(FlowBuiltinType::TextFont)),
("text", "lang") => Some(FlowType::Builtin(FlowBuiltinType::TextLang)),
("text", "region") => Some(FlowType::Builtin(FlowBuiltinType::TextRegion)),
("text" | "stack", "dir") => Some(FlowType::Builtin(FlowBuiltinType::Dir)),
(
// todo: polygon.regular
"page" | "highlight" | "text" | "path" | "rect" | "ellipse" | "circle" | "polygon"
@ -154,6 +156,8 @@ pub(crate) enum FlowBuiltinType {
Color,
TextSize,
TextFont,
TextLang,
TextRegion,
Dir,
Length,
@ -319,3 +323,5 @@ pub static FLOW_RADIUS_DICT: Lazy<FlowRecord> = Lazy::new(|| {
// todo: text.font array
// todo: stroke.dash can be an array
// todo: csv.row-type can be an array or a dictionary
// ISO 639

View file

@ -211,8 +211,8 @@ pub mod typst_to_lsp {
use itertools::Itertools;
use lazy_static::lazy_static;
use lsp_types::{
Command, CompletionTextEdit, Documentation, InsertTextFormat, LanguageString, MarkedString,
MarkupContent, MarkupKind, TextEdit,
Command, CompletionItemLabelDetails, CompletionTextEdit, Documentation, InsertTextFormat,
LanguageString, MarkedString, MarkupContent, MarkupKind, TextEdit,
};
use regex::{Captures, Regex};
use typst::diag::EcoString;
@ -312,6 +312,12 @@ pub mod typst_to_lsp {
label: typst_completion.label.to_string(),
kind: Some(completion_kind(typst_completion.kind.clone())),
detail: typst_completion.detail.as_ref().map(String::from),
label_details: typst_completion.label_detail.as_ref().map(|e| {
CompletionItemLabelDetails {
detail: None,
description: Some(e.to_string()),
}
}),
text_edit: Some(text_edit),
insert_text_format: Some(InsertTextFormat::SNIPPET),
command: typst_completion.command.as_ref().map(|c| Command {

View file

@ -60,6 +60,8 @@ pub struct Completion {
pub kind: CompletionKind,
/// The label the completion is shown with.
pub label: EcoString,
/// The label the completion is shown with.
pub label_detail: Option<EcoString>,
/// The completed version of the input, possibly described with snippet
/// syntax like `${lhs} + ${rhs}`.
///
@ -407,6 +409,7 @@ fn field_access_completions(ctx: &mut CompletionContext, value: &Value, styles:
eco_format!("{method}()${{}}")
}),
detail: None,
label_detail: None,
command: None,
})
}
@ -434,6 +437,7 @@ fn field_access_completions(ctx: &mut CompletionContext, value: &Value, styles:
label: modifier.into(),
apply: None,
detail: None,
label_detail: None,
command: None,
});
}
@ -469,6 +473,7 @@ fn field_access_completions(ctx: &mut CompletionContext, value: &Value, styles:
label: name.clone(),
apply: None,
detail: None,
label_detail: None,
command: None,
})
}
@ -1022,6 +1027,7 @@ impl<'a, 'w> CompletionContext<'a, 'w> {
label: label.into(),
apply: Some(snippet.into()),
detail: Some(docs.into()),
label_detail: None,
// VS Code doesn't do that... Auto triggering suggestion only happens on typing (word
// starts or trigger characters). However, you can use editor.action.triggerSuggest as
// command on a suggestion to "manually" retrigger suggest after inserting one
@ -1082,6 +1088,7 @@ impl<'a, 'w> CompletionContext<'a, 'w> {
label: name.into(),
apply: Some(tags[0].into()),
detail: Some(repr::separated_list(&tags, " or ").into()),
label_detail: None,
command: None,
});
}
@ -1121,6 +1128,7 @@ impl<'a, 'w> CompletionContext<'a, 'w> {
}),
label: label.as_str().into(),
detail,
label_detail: None,
command: None,
});
}
@ -1179,6 +1187,7 @@ impl<'a, 'w> CompletionContext<'a, 'w> {
label,
apply,
detail,
label_detail: None,
command,
});
}
@ -1255,6 +1264,7 @@ impl<'a, 'w> CompletionContext<'a, 'w> {
label: ty.long_name().into(),
apply: Some(eco_format!("${{{ty}}}")),
detail: Some(eco_format!("A value of type {ty}.")),
label_detail: None,
command: None,
});
self.scope_completions(false, |value| value.ty() == *ty);

View file

@ -158,6 +158,7 @@ impl<'a, 'w> CompletionContext<'a, 'w> {
label: name,
apply: Some(apply),
detail: None,
label_detail: None,
// todo: only vscode and neovim (0.9.1) support this
command: Some("editor.action.triggerSuggest"),
});
@ -167,6 +168,7 @@ impl<'a, 'w> CompletionContext<'a, 'w> {
label: name,
apply: None,
detail: None,
label_detail: None,
command: None,
});
}
@ -252,6 +254,7 @@ pub fn param_completions<'a>(
label: param.name.clone().into(),
apply: Some(eco_format!("{}: ${{}}", param.name)),
detail: Some(plain_docs_sentence(&param.docs)),
label_detail: None,
// todo: only vscode and neovim (0.9.1) support this
//
// VS Code doesn't do that... Auto triggering suggestion only happens on typing
@ -397,6 +400,32 @@ fn type_completion(
ctx.strict_scope_completions(false, |value| value.ty() == color_ty);
}
FlowBuiltinType::TextSize => return None,
FlowBuiltinType::TextLang => {
for (&key, desc) in rust_iso639::ALL_MAP.entries() {
let detail = eco_format!("An ISO 639-1/2/3 language code, {}.", desc.name);
ctx.completions.push(Completion {
kind: CompletionKind::Syntax,
label: key.to_lowercase().into(),
apply: Some(eco_format!("\"{}\"", key.to_lowercase())),
detail: Some(detail),
label_detail: Some(desc.name.into()),
command: None,
});
}
}
FlowBuiltinType::TextRegion => {
for (&key, desc) in rust_iso3166::ALPHA2_MAP.entries() {
let detail = eco_format!("An ISO 3166-1 alpha-2 region code, {}.", desc.name);
ctx.completions.push(Completion {
kind: CompletionKind::Syntax,
label: key.to_lowercase().into(),
apply: Some(eco_format!("\"{}\"", key.to_lowercase())),
detail: Some(detail),
label_detail: Some(desc.name.into()),
command: None,
});
}
}
FlowBuiltinType::TextFont => return None,
FlowBuiltinType::Dir => return None,
FlowBuiltinType::Margin => {
@ -467,6 +496,7 @@ fn type_completion(
label: ty.long_name().into(),
apply: Some(eco_format!("${{{ty}}}")),
detail: Some(eco_format!("A value of type {ty}.")),
label_detail: None,
command: None,
});
ctx.strict_scope_completions(false, |value| value.ty() == *ty);
@ -537,6 +567,7 @@ pub fn named_param_value_completions<'a>(
label: expr.clone(),
apply: None,
detail: Some(plain_docs_sentence(&param.docs)),
label_detail: None,
command: None,
});
}
@ -652,6 +683,7 @@ pub fn complete_literal(ctx: &mut CompletionContext) -> Option<()> {
label: key.clone(),
apply: Some(eco_format!("{}: ${{}}", key)),
detail: None,
label_detail: None,
// todo: only vscode and neovim (0.9.1) support this
command: Some("editor.action.triggerSuggest"),
});