11use moonjuice_common:: Position ;
2- use moonjuice_lexer:: { Lexer , Token , TokenValue } ;
3- use std:: fs;
4- use tower_lsp_server:: ls_types:: {
5- SemanticToken , SemanticTokenModifier , SemanticTokenType , SemanticTokens , SemanticTokensLegend , SemanticTokensParams ,
6- SemanticTokensResult ,
7- } ;
2+ use moonjuice_lexer:: { Token , TokenValue } ;
3+ use tower_lsp_server:: ls_types:: { SemanticToken , SemanticTokenModifier , SemanticTokenType , SemanticTokensLegend } ;
84
95enum TokenType {
106 Parameter = 0 ,
@@ -18,98 +14,73 @@ enum TokenType {
1814 Operator = 8 ,
1915}
2016
21- pub struct SemanticHighlightingProvider { }
22-
23- impl SemanticHighlightingProvider {
24- pub fn new ( ) -> Self {
25- SemanticHighlightingProvider { }
17+ fn get_token_type ( token : & Token ) -> Option < TokenType > {
18+ match token. value {
19+ TokenValue :: Nil => Some ( TokenType :: Keyword ) ,
20+ TokenValue :: Bool ( _) => Some ( TokenType :: Keyword ) ,
21+ TokenValue :: Int ( _) => Some ( TokenType :: Number ) ,
22+ TokenValue :: Double ( _) => Some ( TokenType :: Number ) ,
23+ TokenValue :: String ( _, _) => Some ( TokenType :: String ) ,
24+ TokenValue :: Symbol ( _) => Some ( TokenType :: Variable ) ,
25+ TokenValue :: Keyword ( _) => Some ( TokenType :: Keyword ) ,
26+ TokenValue :: Operator ( _) => Some ( TokenType :: Operator ) ,
27+ TokenValue :: SpecialCharacter ( _) => None ,
28+ TokenValue :: Comment ( _) => Some ( TokenType :: Comment ) ,
29+ TokenValue :: UnexpectedCharacter ( _) => None ,
30+ TokenValue :: MalformedNumber ( _) => Some ( TokenType :: Number ) ,
31+ TokenValue :: MalformedString ( _, _) => Some ( TokenType :: String ) ,
2632 }
33+ }
2734
28- pub fn get_legend ( & self ) -> SemanticTokensLegend {
29- SemanticTokensLegend {
30- token_types : vec ! [
31- SemanticTokenType :: PARAMETER ,
32- SemanticTokenType :: VARIABLE ,
33- SemanticTokenType :: PROPERTY ,
34- SemanticTokenType :: FUNCTION ,
35- SemanticTokenType :: KEYWORD ,
36- SemanticTokenType :: COMMENT ,
37- SemanticTokenType :: STRING ,
38- SemanticTokenType :: NUMBER ,
39- SemanticTokenType :: OPERATOR ,
40- ] ,
41- token_modifiers : vec ! [ SemanticTokenModifier :: READONLY ] ,
42- }
35+ pub fn get_legend ( ) -> SemanticTokensLegend {
36+ SemanticTokensLegend {
37+ token_types : vec ! [
38+ SemanticTokenType :: PARAMETER ,
39+ SemanticTokenType :: VARIABLE ,
40+ SemanticTokenType :: PROPERTY ,
41+ SemanticTokenType :: FUNCTION ,
42+ SemanticTokenType :: KEYWORD ,
43+ SemanticTokenType :: COMMENT ,
44+ SemanticTokenType :: STRING ,
45+ SemanticTokenType :: NUMBER ,
46+ SemanticTokenType :: OPERATOR ,
47+ ] ,
48+ token_modifiers : vec ! [ SemanticTokenModifier :: READONLY ] ,
4349 }
50+ }
4451
45- pub fn highlight_full (
46- & self ,
47- params : SemanticTokensParams ,
48- ) -> tower_lsp_server:: jsonrpc:: Result < Option < SemanticTokensResult > > {
49- if let Some ( file_path) = params. text_document . uri . to_file_path ( ) {
50- let contents = fs:: read_to_string ( file_path)
51- . map_err ( |err| tower_lsp_server:: jsonrpc:: Error :: invalid_params ( err. to_string ( ) ) ) ?;
52-
53- let mut previous_start = Position { line : 1 , column : 1 } ;
54- let tokens = Lexer :: tokenise ( contents. chars ( ) . collect ( ) )
55- . into_iter ( )
56- . filter_map ( |token| {
57- let delta_line = if token. start . line != previous_start. line {
58- let delta_line = token. start . line - previous_start. line ;
59-
60- previous_start = Position {
61- line : token. start . line ,
62- column : 1 ,
63- } ;
52+ pub fn convert_to_lsp_tokens < ' a > ( tokens : impl Iterator < Item = & ' a Token > ) -> Vec < SemanticToken > {
53+ let mut previous_start = Position { line : 1 , column : 1 } ;
6454
65- delta_line as u32
66- } else {
67- 0
68- } ;
55+ tokens
56+ . filter_map ( |token| {
57+ let delta_line = if token . start . line != previous_start . line {
58+ let delta_line = token . start . line - previous_start . line ;
6959
70- if let Some ( token_type) = Self :: get_token_type ( & token) {
71- let semantic_token = SemanticToken {
72- delta_line,
73- delta_start : ( token. start . column - previous_start. column ) as u32 ,
74- length : token. lexeme . len ( ) as u32 ,
75- token_type : token_type as u32 ,
76- token_modifiers_bitset : 0 ,
77- } ;
60+ previous_start = Position {
61+ line : token. start . line ,
62+ column : 1 ,
63+ } ;
7864
79- previous_start = token. start ;
80- Some ( semantic_token)
81- } else {
82- None
83- }
84- } )
85- . collect ( ) ;
65+ delta_line as u32
66+ } else {
67+ 0
68+ } ;
8669
87- Ok ( Some ( SemanticTokensResult :: Tokens ( SemanticTokens {
88- result_id : None ,
89- data : tokens,
90- } ) ) )
91- } else {
92- Err ( tower_lsp_server:: jsonrpc:: Error :: invalid_params (
93- "Non-file text document URIs are not supported" ,
94- ) )
95- }
96- }
70+ if let Some ( token_type) = get_token_type ( & token) {
71+ let semantic_token = SemanticToken {
72+ delta_line,
73+ delta_start : ( token. start . column - previous_start. column ) as u32 ,
74+ length : token. lexeme . len ( ) as u32 ,
75+ token_type : token_type as u32 ,
76+ token_modifiers_bitset : 0 ,
77+ } ;
9778
98- fn get_token_type ( token : & Token ) -> Option < TokenType > {
99- match token. value {
100- TokenValue :: Nil => Some ( TokenType :: Keyword ) ,
101- TokenValue :: Bool ( _) => Some ( TokenType :: Keyword ) ,
102- TokenValue :: Int ( _) => Some ( TokenType :: Number ) ,
103- TokenValue :: Double ( _) => Some ( TokenType :: Number ) ,
104- TokenValue :: String ( _, _) => Some ( TokenType :: String ) ,
105- TokenValue :: Symbol ( _) => Some ( TokenType :: Variable ) ,
106- TokenValue :: Keyword ( _) => Some ( TokenType :: Keyword ) ,
107- TokenValue :: Operator ( _) => Some ( TokenType :: Operator ) ,
108- TokenValue :: SpecialCharacter ( _) => None ,
109- TokenValue :: Comment ( _) => Some ( TokenType :: Comment ) ,
110- TokenValue :: UnexpectedCharacter ( _) => None ,
111- TokenValue :: MalformedNumber ( _) => Some ( TokenType :: Number ) ,
112- TokenValue :: MalformedString ( _, _) => Some ( TokenType :: String ) ,
113- }
114- }
79+ previous_start = token. start ;
80+ Some ( semantic_token)
81+ } else {
82+ None
83+ }
84+ } )
85+ . collect ( )
11586}
0 commit comments