-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
104 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright © SixtyFPS GmbH <info@slint.dev> | ||
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 | ||
|
||
use super::token_info::TokenInfo; | ||
use crate::common::DocumentCache; | ||
use i_slint_compiler::langtype::{ElementType, PropertyLookupResult, Type}; | ||
use i_slint_compiler::parser::SyntaxToken; | ||
use itertools::Itertools as _; | ||
use lsp_types::{Hover, HoverContents, MarkupContent}; | ||
|
||
pub fn get_tooltip(document_cache: &mut DocumentCache, token: SyntaxToken) -> Option<Hover> { | ||
let token_info = crate::language::token_info::token_info(document_cache, token)?; | ||
let contents = match token_info { | ||
TokenInfo::Type(ty) => from_plain_text(ty.to_string()), | ||
TokenInfo::ElementType(e) => match e { | ||
ElementType::Component(c) => { | ||
if c.is_global() { | ||
from_slint_code(&format!("global {}", c.id)) | ||
} else { | ||
from_slint_code(&format!("component {}", c.id)) | ||
} | ||
} | ||
ElementType::Builtin(b) => from_plain_text(format!("{} (builtin)", b.name)), | ||
_ => return None, | ||
}, | ||
TokenInfo::ElementRc(e) => { | ||
let e = e.borrow(); | ||
let component = &e.enclosing_component.upgrade().unwrap(); | ||
if component.is_global() { | ||
from_slint_code(&format!("global {}", component.id)) | ||
} else if e.id.is_empty() { | ||
from_slint_code(&format!("{} {{ /*...*/ }}", e.base_type)) | ||
} else { | ||
from_slint_code(&format!("{} := {} {{ /*...*/ }}", e.id, e.base_type)) | ||
} | ||
} | ||
TokenInfo::NamedReference(nr) => { | ||
let prop_info = nr.element().borrow().lookup_property(nr.name()); | ||
from_prop_result(prop_info)? | ||
} | ||
TokenInfo::EnumerationValue(v) => from_slint_code(&format!("{}.{}", v.enumeration.name, v)), | ||
TokenInfo::FileName(_) => return None, | ||
// TODO | ||
TokenInfo::LocalProperty(_) => return None, | ||
TokenInfo::LocalCallback(_) => return None, | ||
TokenInfo::IncompleteNamedReference(el, name) => { | ||
let prop_info = el.lookup_property(&name); | ||
from_prop_result(prop_info)? | ||
} | ||
}; | ||
|
||
Some(Hover { contents: HoverContents::Markup(contents), range: None }) | ||
} | ||
|
||
fn from_prop_result(prop_info: PropertyLookupResult) -> Option<MarkupContent> { | ||
let pure = if prop_info.declared_pure.is_some_and(|x| x) { "pure " } else { "" }; | ||
if let Type::Callback { return_type, args } = &prop_info.property_type { | ||
let ret = return_type.as_ref().map(|x| format!(" -> {}", x)).unwrap_or_default(); | ||
let args = args.iter().map(|x| x.to_string()).join(", "); | ||
Some(from_slint_code(&format!("{pure}callback {}({args}){ret}", prop_info.resolved_name))) | ||
} else if let Type::Function { return_type, args } = &prop_info.property_type { | ||
let ret = if matches!(**return_type, Type::Void) { | ||
String::new() | ||
} else { | ||
format!(" -> {return_type}") | ||
}; | ||
let args = args.iter().map(|x| x.to_string()).join(", "); | ||
Some(from_slint_code(&format!("{pure}function {}({args}){ret}", prop_info.resolved_name))) | ||
} else if prop_info.property_type.is_property_type() { | ||
Some(from_slint_code(&format!( | ||
"property <{}> {}", | ||
prop_info.property_type, prop_info.resolved_name | ||
))) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn from_plain_text(value: String) -> MarkupContent { | ||
MarkupContent { kind: lsp_types::MarkupKind::PlainText, value } | ||
} | ||
|
||
fn from_slint_code(value: &str) -> MarkupContent { | ||
MarkupContent { | ||
kind: lsp_types::MarkupKind::Markdown, | ||
value: format!("```slint\n{value}\n```"), | ||
} | ||
} |