Improved LSP completion ordering
This commit is contained in:
+54
-54
@@ -7,8 +7,8 @@ use std::sync::Arc;
|
|||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
|
||||||
use mist_parser::error::ParseError;
|
use mist_parser::error::ParseError;
|
||||||
use mist_parser::rev_mapper::{Mapping, MistMap, RustMap};
|
|
||||||
use mist_parser::parse;
|
use mist_parser::parse;
|
||||||
|
use mist_parser::rev_mapper::{Mapping, MistMap, RustMap};
|
||||||
use ropey::Rope;
|
use ropey::Rope;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
@@ -22,31 +22,9 @@ use crate::transpiler::transpile_mist;
|
|||||||
static MARKER_COUNTER: AtomicU64 = AtomicU64::new(0);
|
static MARKER_COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
const KEYWORDS: [&'static str; 25] = [
|
const KEYWORDS: [&'static str; 25] = [
|
||||||
"if",
|
"if", "else", "for", "while", "match", "return", "break", "continue", "struct", "enum",
|
||||||
"else",
|
"class", "trait", "impl", "pub", "mut", "let", "true", "false", "dyn", "loop", "fn", "unsafe",
|
||||||
"for",
|
"override", "module", "void",
|
||||||
"while",
|
|
||||||
"match",
|
|
||||||
"return",
|
|
||||||
"break",
|
|
||||||
"continue",
|
|
||||||
"struct",
|
|
||||||
"enum",
|
|
||||||
"class",
|
|
||||||
"trait",
|
|
||||||
"impl",
|
|
||||||
"pub",
|
|
||||||
"mut",
|
|
||||||
"let",
|
|
||||||
"true",
|
|
||||||
"false",
|
|
||||||
"dyn",
|
|
||||||
"loop",
|
|
||||||
"fn",
|
|
||||||
"unsafe",
|
|
||||||
"override",
|
|
||||||
"module",
|
|
||||||
"void"
|
|
||||||
];
|
];
|
||||||
|
|
||||||
fn keyword_completion_items() -> impl Iterator<Item = CompletionItem> {
|
fn keyword_completion_items() -> impl Iterator<Item = CompletionItem> {
|
||||||
@@ -157,8 +135,7 @@ fn inject_marker_at(source: &str, line: u32, character: u32, marker: &str) -> Op
|
|||||||
// Not on an identifier. If the cursor is on a `.` treat it as a field-access
|
// Not on an identifier. If the cursor is on a `.` treat it as a field-access
|
||||||
// and inject the marker as an identifier right after the dot.
|
// and inject the marker as an identifier right after the dot.
|
||||||
if after.starts_with('.') {
|
if after.starts_with('.') {
|
||||||
let dot_end = offset + '.'.
|
let dot_end = offset + '.'.len_utf8();
|
||||||
len_utf8();
|
|
||||||
let mut s = String::with_capacity(source.len() + marker.len() + 4);
|
let mut s = String::with_capacity(source.len() + marker.len() + 4);
|
||||||
s.push_str(&source[..dot_end]);
|
s.push_str(&source[..dot_end]);
|
||||||
s.push_str(marker);
|
s.push_str(marker);
|
||||||
@@ -176,7 +153,6 @@ fn inject_marker_at(source: &str, line: u32, character: u32, marker: &str) -> Op
|
|||||||
Some(s)
|
Some(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn find_marker_position(content: &str, marker: &str) -> Option<Position> {
|
fn find_marker_position(content: &str, marker: &str) -> Option<Position> {
|
||||||
let idx = content.find(marker)?;
|
let idx = content.find(marker)?;
|
||||||
let before = &content[..idx];
|
let before = &content[..idx];
|
||||||
@@ -424,7 +400,10 @@ impl Backend {
|
|||||||
let rust_uri = match clean_lsp_url(&transpiled.rust_path) {
|
let rust_uri = match clean_lsp_url(&transpiled.rust_path) {
|
||||||
Some(u) => u,
|
Some(u) => u,
|
||||||
None => {
|
None => {
|
||||||
eprintln!("[marker] failed to create URI for {:?}", transpiled.rust_path);
|
eprintln!(
|
||||||
|
"[marker] failed to create URI for {:?}",
|
||||||
|
transpiled.rust_path
|
||||||
|
);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -488,8 +467,7 @@ impl Backend {
|
|||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("transpile error for {:?}: {e}", mist_path);
|
eprintln!("transpile error for {:?}: {e}", mist_path);
|
||||||
let diag = transpile_error_to_diagnostic(source).unwrap_or_else(|| {
|
let diag = transpile_error_to_diagnostic(source).unwrap_or_else(|| Diagnostic {
|
||||||
Diagnostic {
|
|
||||||
range: Range {
|
range: Range {
|
||||||
start: Position {
|
start: Position {
|
||||||
line: 0,
|
line: 0,
|
||||||
@@ -504,7 +482,6 @@ impl Backend {
|
|||||||
source: Some("mist".to_string()),
|
source: Some("mist".to_string()),
|
||||||
message: format!("Transpile error: {e}"),
|
message: format!("Transpile error: {e}"),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if let Some(uri) = clean_lsp_url(mist_path) {
|
if let Some(uri) = clean_lsp_url(mist_path) {
|
||||||
self.publish_diagnostics(uri, vec![diag]).await;
|
self.publish_diagnostics(uri, vec![diag]).await;
|
||||||
@@ -909,7 +886,10 @@ impl LanguageServer for Backend {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
else {
|
else {
|
||||||
eprintln!("[completion] resolve_mist_via_marker returned None at ({}, {})", mist_pos.line, mist_pos.character);
|
eprintln!(
|
||||||
|
"[completion] resolve_mist_via_marker returned None at ({}, {})",
|
||||||
|
mist_pos.line, mist_pos.character
|
||||||
|
);
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -942,10 +922,15 @@ impl LanguageServer for Backend {
|
|||||||
cleaned.iter().map(|item| item.label.clone()).collect();
|
cleaned.iter().map(|item| item.label.clone()).collect();
|
||||||
|
|
||||||
cleaned.extend(
|
cleaned.extend(
|
||||||
keyword_completion_items()
|
keyword_completion_items().filter(|item| !existing.contains(&item.label)),
|
||||||
.filter(|item| !existing.contains(&item.label)),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
for item in &mut cleaned {
|
||||||
|
if item.label.starts_with("__") {
|
||||||
|
item.sort_text = Some("zzzz".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Some(CompletionResponse::Array(cleaned)))
|
Ok(Some(CompletionResponse::Array(cleaned)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -957,10 +942,15 @@ impl LanguageServer for Backend {
|
|||||||
cleaned.iter().map(|item| item.label.clone()).collect();
|
cleaned.iter().map(|item| item.label.clone()).collect();
|
||||||
|
|
||||||
cleaned.extend(
|
cleaned.extend(
|
||||||
keyword_completion_items()
|
keyword_completion_items().filter(|item| !existing.contains(&item.label)),
|
||||||
.filter(|item| !existing.contains(&item.label)),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
for item in &mut cleaned {
|
||||||
|
if item.label.starts_with("__") {
|
||||||
|
item.sort_text = Some("zzzz".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
list.items = cleaned;
|
list.items = cleaned;
|
||||||
|
|
||||||
Ok(Some(CompletionResponse::List(list)))
|
Ok(Some(CompletionResponse::List(list)))
|
||||||
@@ -1004,13 +994,18 @@ impl LanguageServer for Backend {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
else {
|
else {
|
||||||
eprintln!("[goto_definition] resolve_mist_via_marker returned None at ({}, {})", mist_pos.line, mist_pos.character);
|
eprintln!(
|
||||||
|
"[goto_definition] resolve_mist_via_marker returned None at ({}, {})",
|
||||||
|
mist_pos.line, mist_pos.character
|
||||||
|
);
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
||||||
let gd_params = GotoDefinitionParams {
|
let gd_params = GotoDefinitionParams {
|
||||||
text_document_position_params: TextDocumentPositionParams {
|
text_document_position_params: TextDocumentPositionParams {
|
||||||
text_document: TextDocumentIdentifier { uri: rust_uri.clone() },
|
text_document: TextDocumentIdentifier {
|
||||||
|
uri: rust_uri.clone(),
|
||||||
|
},
|
||||||
position: rust_pos,
|
position: rust_pos,
|
||||||
},
|
},
|
||||||
work_done_progress_params: WorkDoneProgressParams {
|
work_done_progress_params: WorkDoneProgressParams {
|
||||||
@@ -1049,7 +1044,9 @@ impl LanguageServer for Backend {
|
|||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
if let Some(normalized) = loc.uri.to_file_path().ok().and_then(|p| clean_lsp_url(&p)) {
|
if let Some(normalized) =
|
||||||
|
loc.uri.to_file_path().ok().and_then(|p| clean_lsp_url(&p))
|
||||||
|
{
|
||||||
eprintln!("[goto_definition] returning raw rust loc");
|
eprintln!("[goto_definition] returning raw rust loc");
|
||||||
Ok(Some(GotoDefinitionResponse::Scalar(Location {
|
Ok(Some(GotoDefinitionResponse::Scalar(Location {
|
||||||
uri: normalized,
|
uri: normalized,
|
||||||
@@ -1078,7 +1075,9 @@ impl LanguageServer for Backend {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else if let Some(normalized) = loc.uri.to_file_path().ok().and_then(|p| clean_lsp_url(&p)) {
|
} else if let Some(normalized) =
|
||||||
|
loc.uri.to_file_path().ok().and_then(|p| clean_lsp_url(&p))
|
||||||
|
{
|
||||||
mapped.push(Location {
|
mapped.push(Location {
|
||||||
uri: normalized,
|
uri: normalized,
|
||||||
range: loc.range,
|
range: loc.range,
|
||||||
@@ -1113,8 +1112,11 @@ impl LanguageServer for Backend {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else if let Some(normalized) =
|
} else if let Some(normalized) = link
|
||||||
link.target_uri.to_file_path().ok().and_then(|p| clean_lsp_url(&p))
|
.target_uri
|
||||||
|
.to_file_path()
|
||||||
|
.ok()
|
||||||
|
.and_then(|p| clean_lsp_url(&p))
|
||||||
{
|
{
|
||||||
mapped.push(LocationLink {
|
mapped.push(LocationLink {
|
||||||
origin_selection_range: link.origin_selection_range,
|
origin_selection_range: link.origin_selection_range,
|
||||||
@@ -1161,7 +1163,10 @@ impl LanguageServer for Backend {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
else {
|
else {
|
||||||
eprintln!("[hover] resolve_mist_via_marker returned None at ({}, {})", mist_pos.line, mist_pos.character);
|
eprintln!(
|
||||||
|
"[hover] resolve_mist_via_marker returned None at ({}, {})",
|
||||||
|
mist_pos.line, mist_pos.character
|
||||||
|
);
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1220,8 +1225,8 @@ async fn handle_ra_notifications(
|
|||||||
|
|
||||||
let mist_uri = rust_uri_to_mist_uri(&rust_uri);
|
let mist_uri = rust_uri_to_mist_uri(&rust_uri);
|
||||||
|
|
||||||
let mapped_diagnostics: Vec<Diagnostic> =
|
let mapped_diagnostics: Vec<Diagnostic> = if let Some(ref _mist_uri) = mist_uri
|
||||||
if let Some(ref _mist_uri) = mist_uri {
|
{
|
||||||
let rust_path = rust_uri.to_file_path().ok();
|
let rust_path = rust_uri.to_file_path().ok();
|
||||||
let map_data = match rust_path {
|
let map_data = match rust_path {
|
||||||
Some(ref p) => mapping.lock().await.get(p).cloned(),
|
Some(ref p) => mapping.lock().await.get(p).cloned(),
|
||||||
@@ -1245,9 +1250,7 @@ async fn handle_ra_notifications(
|
|||||||
let final_end = Position {
|
let final_end = Position {
|
||||||
line: mist_end_pos.line.max(mist_start_pos.line),
|
line: mist_end_pos.line.max(mist_start_pos.line),
|
||||||
character: if mist_end_pos.line == mist_start_pos.line {
|
character: if mist_end_pos.line == mist_start_pos.line {
|
||||||
mist_end_pos
|
mist_end_pos.character.max(mist_start_pos.character + 1)
|
||||||
.character
|
|
||||||
.max(mist_start_pos.character + 1)
|
|
||||||
} else {
|
} else {
|
||||||
mist_end_pos.character.max(1)
|
mist_end_pos.character.max(1)
|
||||||
},
|
},
|
||||||
@@ -1432,10 +1435,7 @@ fn compute_mod_decls(
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ensure_implicit_packages_impl(
|
fn ensure_implicit_packages_impl(docs: &mut HashMap<PathBuf, Rope>, src_root: &Path) {
|
||||||
docs: &mut HashMap<PathBuf, Rope>,
|
|
||||||
src_root: &Path,
|
|
||||||
) {
|
|
||||||
let mut dirs_with_mist: HashMap<PathBuf, Vec<PathBuf>> = HashMap::new();
|
let mut dirs_with_mist: HashMap<PathBuf, Vec<PathBuf>> = HashMap::new();
|
||||||
for mist_path in docs.keys() {
|
for mist_path in docs.keys() {
|
||||||
if let Ok(rel) = mist_path.strip_prefix(src_root) {
|
if let Ok(rel) = mist_path.strip_prefix(src_root) {
|
||||||
|
|||||||
Reference in New Issue
Block a user