@@ -6,54 +6,40 @@ use lsp_types::{
66 Position , Range , SemanticToken , SemanticTokenModifier , SemanticTokenType , SemanticTokens ,
77 SemanticTokensLegend , Url ,
88} ;
9- use rowan:: TextRange ;
9+ use rowan:: { TextLen , TextRange } ;
1010
1111use crate :: {
12- db:: Workspace ,
12+ db:: { Document , Workspace } ,
1313 util:: { line_index:: LineIndex , line_index_ext:: LineIndexExt } ,
1414 Db ,
1515} ;
1616
1717#[ derive( Debug , PartialEq , Eq , PartialOrd , Ord , Clone , Copy , Hash ) ]
1818#[ repr( u32 ) ]
19- pub enum TokenKind {
19+ enum TokenKind {
2020 Label = 0 ,
2121 MathDelimiter = 1 ,
2222}
2323
2424bitflags ! {
2525 #[ derive( Debug , PartialEq , Eq , PartialOrd , Ord , Clone , Copy , Hash ) ]
26- pub struct TokenModifiers : u32 {
26+ struct TokenModifiers : u32 {
2727 const NONE = 0 ;
2828 const UNDEFINED = 1 ;
2929 const UNUSED = 2 ;
3030 const DEPRECATED = 4 ;
3131 }
3232}
3333
34- pub fn legend ( ) -> SemanticTokensLegend {
35- SemanticTokensLegend {
36- token_types : vec ! [
37- SemanticTokenType :: new( "label" ) ,
38- SemanticTokenType :: new( "mathDelimiter" ) ,
39- ] ,
40- token_modifiers : vec ! [
41- SemanticTokenModifier :: new( "undefined" ) ,
42- SemanticTokenModifier :: new( "unused" ) ,
43- SemanticTokenModifier :: new( "deprecated" ) ,
44- ] ,
45- }
46- }
47-
4834#[ derive( Debug , PartialEq , Eq , Clone , Copy , Hash ) ]
49- pub struct Token {
50- pub range : TextRange ,
51- pub kind : TokenKind ,
52- pub modifiers : TokenModifiers ,
35+ struct Token {
36+ range : TextRange ,
37+ kind : TokenKind ,
38+ modifiers : TokenModifiers ,
5339}
5440
5541#[ derive( Debug , Default ) ]
56- pub struct TokenBuilder {
42+ struct TokenBuilder {
5743 tokens : Vec < Token > ,
5844}
5945
@@ -106,12 +92,45 @@ impl TokenBuilder {
10692 }
10793}
10894
109- pub fn find_all ( db : & dyn Db , uri : & Url , viewport : Range ) -> Option < SemanticTokens > {
95+ #[ derive( Clone , Copy ) ]
96+ struct Context < ' db > {
97+ db : & ' db dyn Db ,
98+ document : Document ,
99+ viewport : TextRange ,
100+ }
101+
102+ pub fn legend ( ) -> SemanticTokensLegend {
103+ SemanticTokensLegend {
104+ token_types : vec ! [
105+ SemanticTokenType :: new( "label" ) ,
106+ SemanticTokenType :: new( "mathDelimiter" ) ,
107+ ] ,
108+ token_modifiers : vec ! [
109+ SemanticTokenModifier :: new( "undefined" ) ,
110+ SemanticTokenModifier :: new( "unused" ) ,
111+ SemanticTokenModifier :: new( "deprecated" ) ,
112+ ] ,
113+ }
114+ }
115+
116+ pub fn find_all ( db : & dyn Db , uri : & Url , viewport : Option < Range > ) -> Option < SemanticTokens > {
110117 let workspace = Workspace :: get ( db) ;
111118 let document = workspace. lookup_uri ( db, uri) ?;
112- let viewport = document. line_index ( db) . offset_lsp_range ( viewport) ;
119+ let viewport = viewport. map_or_else (
120+ || TextRange :: new ( 0 . into ( ) , document. text ( db) . text_len ( ) ) ,
121+ |range| document. line_index ( db) . offset_lsp_range ( range) ,
122+ ) ;
123+
124+ let context = Context {
125+ db,
126+ document,
127+ viewport,
128+ } ;
129+
113130 let mut builder = TokenBuilder :: default ( ) ;
114- label:: find ( db, document, viewport, & mut builder) ;
115- math_delimiter:: find ( db, document, viewport, & mut builder) ;
131+
132+ label:: find ( context, & mut builder) ;
133+ math_delimiter:: find ( context, & mut builder) ;
134+
116135 Some ( builder. finish ( document. line_index ( db) ) )
117136}
0 commit comments