2024-07-21 15:56:56 +02:00
|
|
|
use std::any::Any;
|
|
|
|
|
2024-10-16 23:42:49 +02:00
|
|
|
use tower_lsp::lsp_types::SemanticToken;
|
|
|
|
use tower_lsp::lsp_types::SemanticTokenType;
|
2024-07-21 15:56:56 +02:00
|
|
|
|
2024-10-16 23:42:49 +02:00
|
|
|
use crate::document::document::Document;
|
|
|
|
use crate::document::element::Element;
|
|
|
|
use crate::elements::comment::Comment;
|
|
|
|
use crate::elements::paragraph::Paragraph;
|
|
|
|
use crate::elements::section::Section;
|
|
|
|
use crate::parser::rule::Rule;
|
2024-07-21 15:56:56 +02:00
|
|
|
|
|
|
|
use super::parser::LineCursor;
|
|
|
|
|
2024-10-16 23:42:49 +02:00
|
|
|
pub trait SemanticProvider: Rule {
|
|
|
|
fn get_semantic_tokens(
|
|
|
|
&self,
|
|
|
|
cursor: &LineCursor,
|
|
|
|
match_data: Box<dyn Any>,
|
|
|
|
) -> Vec<SemanticToken>;
|
2024-07-21 15:56:56 +02:00
|
|
|
}
|
|
|
|
|
2024-10-16 23:42:49 +02:00
|
|
|
pub mod nml_semantic {
|
|
|
|
use tower_lsp::lsp_types::SemanticTokenType;
|
|
|
|
|
|
|
|
pub const SECTION_HEADING: SemanticTokenType = SemanticTokenType::new("type");
|
|
|
|
pub const SECTION_NAME: SemanticTokenType = SemanticTokenType::new("string");
|
|
|
|
pub const REFERENCE: SemanticTokenType = SemanticTokenType::new("event");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const LEGEND_TYPE: &[SemanticTokenType] = &[
|
|
|
|
SemanticTokenType::COMMENT,
|
|
|
|
SemanticTokenType::VARIABLE,
|
|
|
|
SemanticTokenType::STRING,
|
|
|
|
SemanticTokenType::PARAMETER,
|
2024-07-21 15:56:56 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// TODO...
|
2024-10-16 23:42:49 +02:00
|
|
|
pub fn provide(
|
|
|
|
semantic_tokens: &mut Vec<SemanticToken>,
|
|
|
|
cursor: &mut LineCursor,
|
|
|
|
elem: &Box<dyn Element>,
|
|
|
|
) {
|
|
|
|
if cursor.source != elem.location().source() {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-21 15:56:56 +02:00
|
|
|
|
|
|
|
let prev = cursor.clone();
|
|
|
|
|
2024-10-16 23:42:49 +02:00
|
|
|
/*if let Some(comm) = elem.downcast_ref::<Comment>() {
|
2024-07-21 15:56:56 +02:00
|
|
|
cursor.at(elem.location().start());
|
2024-10-16 23:42:49 +02:00
|
|
|
let delta_start = if cursor.line == prev.line {
|
2024-07-21 15:56:56 +02:00
|
|
|
cursor.line_pos - prev.line_pos
|
2024-10-16 23:42:49 +02:00
|
|
|
} else if cursor.line == 0 {
|
|
|
|
cursor.line_pos
|
|
|
|
} else {
|
|
|
|
cursor.line_pos + 1
|
|
|
|
};
|
2024-07-21 15:56:56 +02:00
|
|
|
semantic_tokens.push(SemanticToken {
|
2024-10-16 23:42:49 +02:00
|
|
|
delta_line: (cursor.line - prev.line) as u32,
|
2024-07-21 15:56:56 +02:00
|
|
|
delta_start: delta_start as u32,
|
|
|
|
length: (elem.location().end() - elem.location().start()) as u32,
|
|
|
|
token_type: 0,
|
|
|
|
token_modifiers_bitset: 0,
|
|
|
|
});
|
2024-10-16 23:42:49 +02:00
|
|
|
} else */if let Some(sect) = elem.downcast_ref::<Section>() {
|
|
|
|
cursor.at(elem.location().start() + 1);
|
|
|
|
eprintln!("section {cursor:#?}");
|
|
|
|
let delta_start = if cursor.line == prev.line {
|
2024-07-21 15:56:56 +02:00
|
|
|
cursor.line_pos - prev.line_pos
|
2024-10-16 23:42:49 +02:00
|
|
|
} else if cursor.line == 0 {
|
|
|
|
cursor.line_pos
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
2024-07-21 15:56:56 +02:00
|
|
|
semantic_tokens.push(SemanticToken {
|
2024-10-16 23:42:49 +02:00
|
|
|
delta_line: (cursor.line - prev.line) as u32,
|
2024-07-21 15:56:56 +02:00
|
|
|
delta_start: delta_start as u32,
|
|
|
|
length: (elem.location().end() - elem.location().start()) as u32,
|
|
|
|
token_type: 0,
|
|
|
|
token_modifiers_bitset: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 23:42:49 +02:00
|
|
|
pub fn semantic_token_from_document(document: &dyn Document) -> Vec<SemanticToken> {
|
2024-07-21 15:56:56 +02:00
|
|
|
let mut semantic_tokens = vec![];
|
|
|
|
|
|
|
|
let source = document.source();
|
|
|
|
let mut cursor = LineCursor {
|
|
|
|
pos: 0,
|
|
|
|
line: 0,
|
|
|
|
line_pos: 0,
|
2024-10-16 23:42:49 +02:00
|
|
|
source: source.clone(),
|
2024-07-21 15:56:56 +02:00
|
|
|
};
|
2024-10-16 23:42:49 +02:00
|
|
|
/*
|
2024-07-23 14:04:57 +02:00
|
|
|
semantic_tokens.push(SemanticToken {
|
2024-10-16 23:42:49 +02:00
|
|
|
delta_line: 2,
|
2024-07-23 14:04:57 +02:00
|
|
|
delta_start: 1,
|
|
|
|
length: 5,
|
|
|
|
token_type: 0,
|
|
|
|
token_modifiers_bitset: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
semantic_tokens.push(SemanticToken {
|
|
|
|
delta_line: 1,
|
|
|
|
delta_start: 1,
|
|
|
|
length: 5,
|
|
|
|
token_type: 1,
|
|
|
|
token_modifiers_bitset: 0,
|
2024-10-16 23:42:49 +02:00
|
|
|
});*/
|
2024-07-23 14:04:57 +02:00
|
|
|
|
2024-10-16 23:42:49 +02:00
|
|
|
document.content().borrow()
|
|
|
|
.iter()
|
|
|
|
.for_each(|elem| {
|
|
|
|
if let Some(container) = elem.as_container()
|
|
|
|
{
|
|
|
|
container
|
|
|
|
.contained()
|
|
|
|
.iter()
|
|
|
|
.for_each(|elem| provide(&mut semantic_tokens, &mut cursor, elem));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
provide(&mut semantic_tokens, &mut cursor, elem);
|
|
|
|
}
|
|
|
|
});
|
2024-07-21 15:56:56 +02:00
|
|
|
|
|
|
|
semantic_tokens
|
|
|
|
}
|