Skip to content

Commit

Permalink
chore: use strum to iterate enum fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Dec 21, 2023
1 parent 62bc974 commit d7fa413
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 39 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ tokio-util = { version = "*", features = ["codec"] }
serde_json = "*"
tracing = "*"
tracing-subscriber = { version = "*", features = ["env-filter"] }
once_cell = "*"
once_cell = "*"
strum = "*"
strum_macros = "*"
50 changes: 12 additions & 38 deletions lsp/src/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,32 @@
use crate::lsp_types::{TokenModifiers, TokenTypes};

use serde_json::Value;
use stc::parser::{StLexerBuilder, Tok};
use strum::IntoEnumIterator;
use tower_lsp::jsonrpc;
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer};
use tracing::*;

const TOKEN_MODIFIERS: &[SemanticTokenModifier] = &[
SemanticTokenModifier::new("none"),
// 1, STATIC
SemanticTokenModifier::STATIC,
// 2, GLOBAL
SemanticTokenModifier::new("global"),
// 4, RETAIN
SemanticTokenModifier::new("retain"),
];

const TOKEN_TYPES: &[SemanticTokenType] = &[
// 0, NONE
SemanticTokenType::new("none"),
// 1, keywords
SemanticTokenType::KEYWORD,
// 2, identifiers
SemanticTokenType::VARIABLE,
// 3, operators
SemanticTokenType::OPERATOR,
// 4, builtin functions
SemanticTokenType::new("builtin-functions"),
// 5, number literals
SemanticTokenType::NUMBER,
// 6, string literals
SemanticTokenType::STRING,
// 7, type
SemanticTokenType::TYPE,
];

fn semantic_token_type_id(tok: &Tok) -> (u32, u32) {
match tok {
Tok::Identifier(_) => (2, 0),
Tok::Literal(_) => (5, 0),
Tok::String => (6, 0),
Tok::Identifier(_) => (TokenTypes::Variable as u32, TokenModifiers::None as u32),
Tok::Literal(_) => (TokenTypes::Number as u32, TokenModifiers::None as u32),
Tok::String => (TokenTypes::String as u32, TokenModifiers::None as u32),
// operators
op if op.is_operator() => (3, 0),
op if op.is_operator() => (TokenTypes::Operator as u32, TokenModifiers::None as u32),
// builtin-types
Tok::Int => (7, 0),
Tok::Int => (TokenTypes::Type as u32, TokenModifiers::None as u32),
// keywords
Tok::If
| Tok::Then
| Tok::EndIf
| Tok::Var
| Tok::EndVar
| Tok::Program
| Tok::EndProgram => (1, 0),
_ => (0, 0),
| Tok::EndProgram => (TokenTypes::Keyword as u32, TokenModifiers::None as u32),
_ => (TokenTypes::None as u32, TokenModifiers::None as u32),
}
}

Expand All @@ -74,8 +48,8 @@ impl LanguageServer for StcLsp {
work_done_progress: None,
},
legend: SemanticTokensLegend {
token_types: TOKEN_TYPES.to_vec(),
token_modifiers: TOKEN_MODIFIERS.to_vec(),
token_types: TokenTypes::iter().map(Into::into).collect(),
token_modifiers: TokenModifiers::iter().map(Into::into).collect(),
},
range: Some(true),
full: Some(SemanticTokensFullOptions::Delta { delta: Some(true) }),
Expand Down
46 changes: 46 additions & 0 deletions lsp/src/lsp_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use strum_macros::{Display, EnumIter};
use tower_lsp::lsp_types::{SemanticTokenModifier, SemanticTokenType};

#[derive(Debug, PartialEq, Eq, EnumIter, Display)]
#[repr(u32)]
pub enum TokenTypes {
None,
Keyword,
Variable,
Operator,
BuiltinFunction,
Number,
String,
Type,
}

impl From<TokenTypes> for SemanticTokenType {
fn from(value: TokenTypes) -> Self {
match value {
TokenTypes::None => SemanticTokenType::new("none"),
TokenTypes::Keyword => SemanticTokenType::KEYWORD,
TokenTypes::Variable => SemanticTokenType::VARIABLE,
TokenTypes::Operator => SemanticTokenType::OPERATOR,
TokenTypes::BuiltinFunction => SemanticTokenType::new("builtin-functions"),
TokenTypes::Number => SemanticTokenType::NUMBER,
TokenTypes::String => SemanticTokenType::STRING,
TokenTypes::Type => SemanticTokenType::TYPE,
}
}
}

#[derive(Debug, PartialEq, Eq, EnumIter, Display)]
#[repr(u32)]
pub enum TokenModifiers {
None,
Static,
}

impl From<TokenModifiers> for SemanticTokenModifier {
fn from(value: TokenModifiers) -> Self {
match value {
TokenModifiers::None => SemanticTokenModifier::new("none"),
TokenModifiers::Static => SemanticTokenModifier::STATIC,
}
}
}
1 change: 1 addition & 0 deletions lsp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod lsp;
mod lsp_types;

use lsp::StcLsp;
use std::io;
Expand Down

0 comments on commit d7fa413

Please sign in to comment.