@@ -24,7 +24,7 @@ import {
24
24
import { Position , TextDocument } from "vscode-languageserver-textdocument" ;
25
25
import type { SourceNode } from "source-map-generator" ;
26
26
import { WatchMap } from "./watchMap" ;
27
- import { debounce } from "../common/debounce" ;
27
+ import { debouncePromise } from "../common/debounce" ;
28
28
29
29
function getSession (
30
30
ast : peggy . ast . Grammar ,
@@ -36,6 +36,7 @@ function getSession(
36
36
ast . code = session as unknown as SourceNode ;
37
37
}
38
38
39
+ const ID = "peggyLanguageServer" ;
39
40
const AST = new WatchMap < string , any > ( ) ;
40
41
const WORD_RE = / [ ^ \s { } [ \] ( ) ` ~ ! @ # % ^ & * + \- = | \\ ; : ' " , . / < > ? ] + / g;
41
42
const PASSES : peggy . compiler . Stages = {
@@ -60,6 +61,19 @@ const documents = new TextDocuments(TextDocument);
60
61
// for open, change and close text document events
61
62
documents . listen ( connection ) ;
62
63
64
+ interface PeggySettings {
65
+ consoleInfo : boolean ;
66
+ markInfo : boolean ;
67
+ debounceMS : number ;
68
+ }
69
+
70
+ const defaultSettings : PeggySettings = {
71
+ consoleInfo : false ,
72
+ markInfo : true ,
73
+ debounceMS : 200 ,
74
+ } ;
75
+ let globalSettings : PeggySettings = defaultSettings ;
76
+
63
77
// After the server has started the client sends an initilize request. The
64
78
// server receives in the passed params the rootPath of the workspace plus the
65
79
// client capabilites.
@@ -77,16 +91,9 @@ connection.onInitialize((): InitializeResult => ({
77
91
} ,
78
92
} ) ) ;
79
93
80
- interface PeggySettings {
81
- consoleInfo : boolean ;
82
- markInfo : boolean ;
83
- }
84
-
85
- const defaultSettings : PeggySettings = {
86
- consoleInfo : false ,
87
- markInfo : true ,
88
- } ;
89
- let globalSettings : PeggySettings = defaultSettings ;
94
+ connection . onInitialized ( async ( ) => {
95
+ globalSettings = await connection . workspace . getConfiguration ( ID ) ;
96
+ } ) ;
90
97
91
98
function peggyLoc_to_vscodeRange ( loc : peggy . LocationRange ) : Range {
92
99
if ( ! loc ) {
@@ -143,7 +150,7 @@ function addProblemDiagnostics(
143
150
}
144
151
}
145
152
146
- const validateTextDocument = debounce ( ( doc : TextDocument ) : void = > {
153
+ async function validate ( doc : TextDocument ) : Promise < void > {
147
154
AST . delete ( doc . uri ) ; // Cancel any pending and start over.
148
155
149
156
const diagnostics : Diagnostic [ ] = [ ] ;
@@ -185,13 +192,17 @@ const validateTextDocument = debounce((doc: TextDocument): void => {
185
192
}
186
193
187
194
// Send the computed diagnostics to VS Code.
188
- connection . sendDiagnostics ( { uri : doc . uri , diagnostics } ) ;
189
- } , 150 ) ;
195
+ await connection . sendDiagnostics ( { uri : doc . uri , diagnostics } ) ;
196
+ }
190
197
191
- connection . onDidChangeConfiguration ( change => {
192
- if ( change . settings ) {
193
- globalSettings = change . settings . peggyLanguageServer as PeggySettings ;
194
- }
198
+ const validateTextDocument = debouncePromise (
199
+ validate ,
200
+ globalSettings . debounceMS
201
+ ) ;
202
+
203
+ connection . onDidChangeConfiguration ( async ( ) => {
204
+ globalSettings = await connection . workspace . getConfiguration ( ID ) ;
205
+ validateTextDocument . wait = globalSettings . debounceMS ;
195
206
196
207
// Revalidate all open text documents
197
208
documents . all ( ) . forEach ( validateTextDocument ) ;
0 commit comments