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

90
Cargo.lock generated
View file

@ -625,7 +625,7 @@ version = "0.15.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb"
dependencies = [
"encode_unicode",
"encode_unicode 0.3.6",
"lazy_static",
"libc",
"windows-sys 0.52.0",
@ -925,6 +925,16 @@ dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-next"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1"
dependencies = [
"cfg-if",
"dirs-sys-next",
]
[[package]]
name = "dirs-sys"
version = "0.4.1"
@ -937,6 +947,17 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "displaydoc"
version = "0.2.4"
@ -1014,6 +1035,12 @@ version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "encode_unicode"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
[[package]]
name = "encoding_rs"
version = "0.8.33"
@ -1909,6 +1936,17 @@ dependencies = [
"once_cell",
]
[[package]]
name = "is-terminal"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "is-wsl"
version = "0.4.0"
@ -2633,6 +2671,20 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "prettytable-rs"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eea25e07510aa6ab6547308ebe3c036016d162b8da920dbb079e3ba8acf3d95a"
dependencies = [
"csv",
"encode_unicode 1.0.0",
"is-terminal",
"lazy_static",
"term",
"unicode-width",
]
[[package]]
name = "proc-macro2"
version = "1.0.79"
@ -2961,6 +3013,29 @@ version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cd14fd5e3b777a7422cca79358c57a8f6e3a703d9ac187448d0daf220c2407f"
[[package]]
name = "rust_iso3166"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e676677b94480848a8d88c74553bad50aed2ee77d8c0985aa50d8c4e26f3054b"
dependencies = [
"js-sys",
"phf",
"prettytable-rs",
"wasm-bindgen",
]
[[package]]
name = "rust_iso639"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a079fa995fd99d42d7244f325b6737677852036ce2efe5244227d2c75ff4bd73"
dependencies = [
"phf",
"prettytable-rs",
"wasm-bindgen",
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"
@ -3574,6 +3649,17 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "term"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f"
dependencies = [
"dirs-next",
"rustversion",
"winapi",
]
[[package]]
name = "termcolor"
version = "1.4.1"
@ -3764,6 +3850,8 @@ dependencies = [
"percent-encoding",
"reflexo",
"regex",
"rust_iso3166",
"rust_iso639",
"serde",
"serde_json",
"sha2",

View file

@ -40,6 +40,8 @@ percent-encoding = "2"
unscanny = "0.1"
pathdiff = "0.2"
ttf-parser = "0.20.0"
rust_iso639 = "0.0.1"
rust_iso3166 = "0.1.4"
[dev-dependencies]
once_cell.workspace = true

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"),
});