Skip to content

Commit

Permalink
unify redundant to_lsp_snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Song-Nop committed Dec 12, 2024
1 parent fd37a5e commit e69a8a9
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 21 deletions.
20 changes: 1 addition & 19 deletions crates/tinymist-query/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ use lsp_types::{
Command, CompletionItemLabelDetails, CompletionList, CompletionTextEdit, InsertTextFormat,
TextEdit,
};
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
use typst_shim::syntax::LinkedNodeExt;

use crate::{
analysis::{InsTy, Ty},
prelude::*,
syntax::{is_ident_like, DerefTarget},
upstream::{autocomplete, CompletionContext},
upstream::{autocomplete, to_lsp_snippet, CompletionContext},
StatefulRequest,
};

Expand Down Expand Up @@ -275,22 +273,6 @@ pub(crate) fn completion_kind(typst_completion_kind: TypstCompletionKind) -> Lsp
}
}

static TYPST_SNIPPET_PLACEHOLDER_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\$\{(.*?)\}").unwrap());

/// Adds numbering to placeholders in snippets
fn to_lsp_snippet(typst_snippet: &EcoString) -> String {
let mut counter = 1;
let result =
TYPST_SNIPPET_PLACEHOLDER_RE.replace_all(typst_snippet.as_str(), |cap: &Captures| {
let substitution = format!("${{{}:{}}}", counter, &cap[1]);
counter += 1;
substitution
});

result.to_string()
}

fn is_arg_like_context(mut matching: &LinkedNode) -> bool {
while let Some(parent) = matching.parent() {
use SyntaxKind::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/tinymist-query/src/upstream/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use crate::snippet::{
use crate::syntax::InterpretMode;

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

/// Autocomplete a cursor position in a source file.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/tinymist-query/src/upstream/complete/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ static DEFAULT_POSTFIX_SNIPPET: LazyLock<Vec<PostfixSnippet>> = LazyLock::new(||
static TYPST_SNIPPET_PLACEHOLDER_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\$\{(.*?)\}").unwrap());
/// Adds numbering to placeholders in snippets
fn to_lsp_snippet(typst_snippet: &EcoString) -> String {
pub fn to_lsp_snippet(typst_snippet: &EcoString) -> String {
let mut counter = 1;
let result =
TYPST_SNIPPET_PLACEHOLDER_RE.replace_all(typst_snippet.as_str(), |cap: &Captures| {
Expand Down

0 comments on commit e69a8a9

Please sign in to comment.