@@ -35,7 +35,12 @@ func (s *lspServer) initialized(context *glsp.Context, params *lsp.InitializedPa
3535}
3636
3737func (s * lspServer ) shutdown (context * glsp.Context ) error {
38+ if s .refresh != nil {
39+ s .refresh .Stop ()
40+ }
41+
3842 lsp .SetTraceValue (lsp .TraceValueOff )
43+
3944 return nil
4045}
4146
@@ -53,8 +58,25 @@ func (s *lspServer) loadMixins(uri string) {
5358
5459 path := normalizePath (uri )
5560
56- for _ , filename := range s .parser .getMixinFilenames (doc ) {
61+ currentMixins := s .parser .getMixinFilenames (doc )
62+ previousMixins := s .storage .GetMixins (uri )
63+
64+ s .storage .SetMixins (uri , currentMixins )
65+
66+ for _ , filename := range currentMixins {
67+ if slices .Contains (previousMixins , filename ) {
68+ continue
69+ }
70+
5771 mixinPath := replacePathFilename (path , strings .TrimPrefix (filename , "-" ))
72+ mixinURI := pathToURI (mixinPath )
73+
74+ // skip if the document is already managed by the storage
75+ // e.g. opened manually in the editor or already loaded
76+ if s .storage .GetDocument (mixinURI ) != nil {
77+ continue
78+ }
79+
5880 if ! util .FileExists (mixinPath ) {
5981 s .log .Debugf ("mixin target does not exist: %s" , mixinPath )
6082 continue
@@ -66,19 +88,29 @@ func (s *lspServer) loadMixins(uri string) {
6688 continue
6789 }
6890
69- mixinURI := pathToURI (mixinPath )
91+ s .log .Debugf ("load mixin %s" , mixinPath )
92+
7093 text := string (data )
7194
7295 s .storage .AddDocument (mixinURI , text )
7396 s .index .IndexDocument (mixinURI , text )
7497 }
7598}
7699
100+ func (s * lspServer ) refreshDocument (uri string ) {
101+ doc := s .storage .GetDocument (uri )
102+ if doc == nil {
103+ return
104+ }
105+
106+ s .index .IndexDocument (uri , * doc )
107+ s .loadMixins (uri )
108+ }
109+
77110func (s * lspServer ) textDocumentDidOpen (context * glsp.Context , params * lsp.DidOpenTextDocumentParams ) error {
78111 s .storage .AddDocument (params .TextDocument .URI , params .TextDocument .Text )
79112
80- go s .index .IndexDocument (params .TextDocument .URI , params .TextDocument .Text )
81- go s .loadMixins (params .TextDocument .URI )
113+ go s .refreshDocument (params .TextDocument .URI )
82114
83115 return nil
84116}
@@ -89,8 +121,11 @@ func (s *lspServer) textDocumentDidChange(context *glsp.Context, params *lsp.Did
89121 case lsp.TextDocumentContentChangeEventWhole :
90122 s .storage .AddDocument (params .TextDocument .URI , c .Text )
91123
92- go s .index .IndexDocument (params .TextDocument .URI , c .Text )
93- go s .loadMixins (params .TextDocument .URI )
124+ if s .refresh != nil {
125+ s .refresh .Schedule (params .TextDocument .URI )
126+ } else {
127+ go s .refreshDocument (params .TextDocument .URI )
128+ }
94129 case lsp.TextDocumentContentChangeEvent :
95130 return errors .New ("incremental changes not supported" )
96131 }
0 commit comments