Skip to content

Commit

Permalink
allow disabling custom token types
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jun 6, 2024
1 parent e71cd73 commit 2589ace
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This version is not yet released. If you are reading this on the website, then t
### Interpreter
- Some optimizations
- Array shapes now show on hover in the LSP
- Allow Uiua-specific tokens types to be disabled in the LSP

## 0.11.1 - 2024-06-06
### Interpreter
Expand Down
45 changes: 42 additions & 3 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ mod server {
const TRIADIC_MODIFIER_STT: SemanticTokenType = SemanticTokenType::new("triadic_modifier");
const MODULE_STT: SemanticTokenType = SemanticTokenType::new("module");

const SEMANTIC_TOKEN_TYPES: [SemanticTokenType; 13] = [
const UIUA_SEMANTIC_TOKEN_TYPES: [SemanticTokenType; 13] = [
SemanticTokenType::COMMENT,
UIUA_NUMBER_STT,
UIUA_STRING_STT,
Expand All @@ -591,6 +591,23 @@ mod server {
MODULE_STT,
];

const NO_STT: SemanticTokenType = SemanticTokenType::new("none");
const GENERIC_SEMANTIC_TOKEN_TYPES: [SemanticTokenType; 13] = [
SemanticTokenType::COMMENT,
SemanticTokenType::NUMBER,
SemanticTokenType::STRING,
NO_STT,
SemanticTokenType::PROPERTY,
SemanticTokenType::CLASS,
SemanticTokenType::METHOD,
NO_STT,
NO_STT,
SemanticTokenType::TYPE,
SemanticTokenType::KEYWORD,
NO_STT,
SemanticTokenType::NAMESPACE,
];

#[tower_lsp::async_trait]
impl LanguageServer for Backend {
async fn initialize(&self, _params: InitializeParams) -> Result<InitializeResult> {
Expand Down Expand Up @@ -627,7 +644,10 @@ mod server {
SemanticTokensOptions {
work_done_progress_options: WorkDoneProgressOptions::default(),
legend: SemanticTokensLegend {
token_types: SEMANTIC_TOKEN_TYPES.to_vec(),
token_types: UIUA_SEMANTIC_TOKEN_TYPES
.into_iter()
.chain(GENERIC_SEMANTIC_TOKEN_TYPES)
.collect(),
token_modifiers: vec![],
},
range: Some(false),
Expand Down Expand Up @@ -1174,6 +1194,22 @@ mod server {
let Some(doc) = self.docs.get(&params.text_document.uri) else {
return Ok(None);
};

let config = self
.client
.configuration(vec![ConfigurationItem {
scope_uri: Some(params.text_document.uri.clone()),
section: Some("uiua.semanticHighlighting.customTokenTypes".into()),
}])
.await
.unwrap_or_default();
let custom_highlighting = if let [serde_json::Value::Bool(enabled)] = config.as_slice()
{
*enabled
} else {
true
};

let mut tokens = Vec::new();
let mut prev_line = 0;
let mut prev_char = 0;
Expand Down Expand Up @@ -1224,10 +1260,13 @@ mod server {
SpanKind::ArraySwizzle(_) => MONADIC_FUNCTION_STT,
_ => continue,
};
let token_type = SEMANTIC_TOKEN_TYPES
let mut token_type = UIUA_SEMANTIC_TOKEN_TYPES
.iter()
.position(|t| t == &token_type)
.unwrap() as u32;
if !custom_highlighting {
token_type += UIUA_SEMANTIC_TOKEN_TYPES.len() as u32;
}
let span = &sp.span;
let start = uiua_loc_to_lsp(span.start);
let delta_start = if start.line == prev_line {
Expand Down
3 changes: 0 additions & 3 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
- Macro args doc comments
- Fix pad cursor jumping with end-of-line comments
- Array bar notation
- Tree extension?
- Show array shape on hover
- Unicode literals
- `fill windows`
- Error classes
- `# Track caller!`
- `base` function
Expand Down

0 comments on commit 2589ace

Please sign in to comment.