mirror of
https://github.com/slint-ui/slint.git
synced 2025-11-20 11:56:14 +00:00
Make LSP command creation a bit DRYer
This commit is contained in:
parent
affaf844af
commit
a68eb876eb
1 changed files with 24 additions and 18 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
|
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
|
||||||
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
|
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
|
||||||
|
|
||||||
|
// cSpell: ignore descr rfind
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
use crate::wasm_prelude::*;
|
use crate::wasm_prelude::*;
|
||||||
use crate::{completion, goto, semantic_tokens, util, RequestHolder};
|
use crate::{completion, goto, semantic_tokens, util, RequestHolder};
|
||||||
|
|
@ -27,6 +29,18 @@ pub type Error = Box<dyn std::error::Error>;
|
||||||
|
|
||||||
const SHOW_PREVIEW_COMMAND: &str = "showPreview";
|
const SHOW_PREVIEW_COMMAND: &str = "showPreview";
|
||||||
|
|
||||||
|
fn create_show_preview_command(pretty: bool, file: &str, component_name: &str) -> Option<Command> {
|
||||||
|
if !cfg!(feature = "preview") && !cfg!(target_arch = "wasm32") {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let title = format!("{}Show Preview", if pretty { &"▶ " } else { &"" });
|
||||||
|
Some(Command::new(
|
||||||
|
title,
|
||||||
|
SHOW_PREVIEW_COMMAND.into(),
|
||||||
|
Some(vec![file.into(), component_name.into()]),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
pub struct DocumentCache {
|
pub struct DocumentCache {
|
||||||
pub(crate) documents: TypeLoader,
|
pub(crate) documents: TypeLoader,
|
||||||
newline_offsets: HashMap<Url, Vec<u32>>,
|
newline_offsets: HashMap<Url, Vec<u32>>,
|
||||||
|
|
@ -367,10 +381,6 @@ fn get_code_actions(
|
||||||
_document_cache: &mut DocumentCache,
|
_document_cache: &mut DocumentCache,
|
||||||
node: SyntaxNode,
|
node: SyntaxNode,
|
||||||
) -> Option<Vec<CodeActionOrCommand>> {
|
) -> Option<Vec<CodeActionOrCommand>> {
|
||||||
if !cfg!(feature = "preview") && !cfg!(target_arch = "wasm32") {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let component = syntax_nodes::Component::new(node.clone())
|
let component = syntax_nodes::Component::new(node.clone())
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
syntax_nodes::DeclaredIdentifier::new(node.clone())
|
syntax_nodes::DeclaredIdentifier::new(node.clone())
|
||||||
|
|
@ -388,11 +398,11 @@ fn get_code_actions(
|
||||||
let component_name =
|
let component_name =
|
||||||
i_slint_compiler::parser::identifier_text(&component.DeclaredIdentifier())?;
|
i_slint_compiler::parser::identifier_text(&component.DeclaredIdentifier())?;
|
||||||
|
|
||||||
Some(vec![CodeActionOrCommand::Command(Command::new(
|
Some(vec![CodeActionOrCommand::Command(create_show_preview_command(
|
||||||
"Show preview".into(),
|
false,
|
||||||
SHOW_PREVIEW_COMMAND.into(),
|
&component.source_file.path().to_string_lossy(),
|
||||||
Some(vec![component.source_file.path().to_string_lossy().into(), component_name.into()]),
|
&component_name,
|
||||||
))])
|
)?)])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_document_color(
|
fn get_document_color(
|
||||||
|
|
@ -489,10 +499,6 @@ fn get_code_lenses(
|
||||||
document_cache: &mut DocumentCache,
|
document_cache: &mut DocumentCache,
|
||||||
text_document: &lsp_types::TextDocumentIdentifier,
|
text_document: &lsp_types::TextDocumentIdentifier,
|
||||||
) -> Option<Vec<CodeLens>> {
|
) -> Option<Vec<CodeLens>> {
|
||||||
if !cfg!(feature = "preview") && !cfg!(target_arch = "wasm32") {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let uri = &text_document.uri;
|
let uri = &text_document.uri;
|
||||||
let filepath = uri.to_file_path().ok()?;
|
let filepath = uri.to_file_path().ok()?;
|
||||||
let doc = document_cache.documents.get_document(&filepath)?;
|
let doc = document_cache.documents.get_document(&filepath)?;
|
||||||
|
|
@ -512,11 +518,11 @@ fn get_code_lenses(
|
||||||
.filter_map(|c| {
|
.filter_map(|c| {
|
||||||
Some(CodeLens {
|
Some(CodeLens {
|
||||||
range: make_range(c.root_element.borrow().node.as_ref()?)?,
|
range: make_range(c.root_element.borrow().node.as_ref()?)?,
|
||||||
command: Some(Command::new(
|
command: Some(create_show_preview_command(
|
||||||
"▶ Show preview".into(),
|
true,
|
||||||
SHOW_PREVIEW_COMMAND.into(),
|
filepath.to_str()?,
|
||||||
Some(vec![filepath.to_str()?.into(), c.id.as_str().into()]),
|
c.id.as_str(),
|
||||||
)),
|
)?),
|
||||||
data: None,
|
data: None,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue