@@ -4,18 +4,21 @@ const keywords = require('./keywords.js');
44
55var g_client ;
66var g_updateStatusBar ;
7- var g_timerConnect ; // 重连定时器
87var g_timerApply ; // 延迟应用样式
9- var g_autoApply = true ; // 是否启用自动应用样式
108var g_valid = true ;
11- var g_host = "localhost" ;
9+ var g_tmpDoc ; // 临时文档
10+ var g_host = 'localhost' ;
1211var g_port = 61052 ;
12+ var g_count = 0 ;
13+ var g_output ;
14+ var g_styles = [ ] ; // 样式内容
1315
1416/**
1517 * 初始化关键词
1618 * @param {vscode.ExtensionContext } context
1719 */
1820function initKeywords ( context ) {
21+ g_output . appendLine ( 'NodeClient::initKeywords' ) ;
1922 keywords . register ( context ) ;
2023} ;
2124
@@ -25,90 +28,162 @@ function initKeywords(context) {
2528function initAddress ( ) {
2629 g_host = vscode . workspace . getConfiguration ( ) . get ( 'qsseditor.serverHost' , '127.0.0.1' ) ;
2730 g_port = Number ( vscode . workspace . getConfiguration ( ) . get ( 'qsseditor.serverPort' , '61052' ) ) ;
31+ g_output . appendLine ( `NodeClient::initAddress, host=${ g_host } , port=${ g_port } ` ) ;
2832} ;
2933
3034/**
3135 * 启动客户端
3236 */
3337function startClient ( ) {
34- console . log ( 'NodeClient::start' ) ;
35- if ( g_timerConnect != undefined ) {
36- clearTimeout ( g_timerConnect ) ;
37- g_timerConnect = undefined ;
38- }
39- if ( ! g_valid ) return ;
38+ if ( g_client != undefined ) return ;
39+ g_output . appendLine ( 'NodeClient::startClient' ) ;
4040
4141 initAddress ( ) ;
4242 g_client = new RpcWebSocket ( 'ws://' + g_host + ':' + g_port , {
43- reconnect : false
43+ autoconnect : true ,
44+ reconnect : true ,
45+ reconnect_interval : 3000 ,
46+ max_reconnects : 0 , // 无限重连
4447 } ) ;
4548 g_client . on ( 'open' , function ( ) {
46- console . log ( 'NodeClient::handleConnected' ) ;
49+ g_output . appendLine ( 'NodeClient::handleConnected' ) ;
50+ g_count = 0 ;
51+ setValid ( true ) ;
4752 if ( g_updateStatusBar ) g_updateStatusBar ( true ) ;
4853 } ) ;
4954 g_client . on ( 'close' , function ( ) {
50- console . log ( 'NodeClient::handleDisconnected' ) ;
51- if ( g_valid && g_timerConnect == undefined ) g_timerConnect = setTimeout ( startClient , 3000 ) ;
55+ if ( g_count < 10 ) {
56+ g_output . appendLine ( 'NodeClient::handleDisconnected' ) ;
57+ }
58+ g_count ++ ;
59+ setValid ( false ) ;
5260 if ( g_updateStatusBar ) g_updateStatusBar ( false ) ;
5361 } ) ;
5462 g_client . on ( 'error' , function ( event ) {
55- console . error ( 'NodeClient::handleError: ' + event . error ) ;
56- if ( g_valid && g_timerConnect == undefined ) g_timerConnect = setTimeout ( startClient , 3000 ) ;
63+ if ( g_count < 10 ) {
64+ g_output . appendLine ( 'NodeClient::handleError: ' + event . error ) ;
65+ }
66+ g_count ++ ;
67+ setValid ( false ) ;
5768 if ( g_updateStatusBar ) g_updateStatusBar ( false ) ;
5869 } ) ;
5970 g_client . on ( 'addKeywords' , function ( words ) {
60- console . log ( 'NodeClient::handleKeywordAdd: name=' + words ) ;
71+ g_output . appendLine ( 'NodeClient::handleKeywordAdd: name=' + words ) ;
6172 words . forEach ( keywords . add ) ;
6273 } ) ;
6374} ;
6475
76+
6577/**
6678 * 关闭客户端
6779 */
6880function stopClient ( ) {
69- console . log ( 'NodeClient::stop' ) ;
70- g_valid = false ;
71- if ( g_client != undefined ) g_client . close ( ) ;
81+ g_output . appendLine ( 'NodeClient::stopClient' ) ;
82+ setValid ( false ) ;
83+ if ( g_client != undefined ) {
84+ g_client . setAutoReconnect ( false ) ;
85+ g_client . close ( ) ;
86+ }
7287} ;
7388
7489/**
75- * 选中控件
90+ * 设置插件是否激活可用标志
91+ * @param {boolean } valid
7692 */
77- function selectWidget ( word ) {
78- if ( g_client == undefined ) return ;
79- g_client . notify ( 'selectWidget' , [ word ] ) ;
80- } ;
93+ function setValid ( valid ) {
94+ // g_output.appendLine('NodeClient::setValid, valid=' + valid) ;
95+ g_valid = valid ;
96+ }
8197
8298/**
83- * 应用样式
99+ * 设置窗口输出
100+ * @param {vscode.OutputChannel } output
101+ */
102+ function setOutputChannel ( output ) {
103+ g_output = output ;
104+ }
105+
106+ /**
107+ * 更新样式内容
108+ * @param {vscode.TextDocument } document
84109 */
85- function applyStyle ( doc ) {
86- if ( g_client == undefined ) return ;
87- // 获取当前激活的编辑器
88- const editor = vscode . window . activeTextEditor ;
89- let document = editor . document ;
110+ function updateStyleText ( document ) {
111+ // g_output.appendLine('NodeClient::updateStyleText');
112+ g_styles = [ ] ;
113+ if ( ! g_valid || g_client == undefined ) return ;
90114
91- if ( doc == undefined ) {
115+ // g_output.appendLine(`NodeClient::updateStyleText, document=${document}`);
116+
117+ if ( document == undefined ) {
118+ // 获取当前激活的编辑器
119+ const editor = vscode . window . activeTextEditor ;
92120 if ( editor == undefined ) return ;
93- } else {
94- if ( doc != document ) return ;
95- document = doc ;
121+ document = editor . document ;
122+ }
123+
124+ try {
125+ // 获取选中的内容
126+ const selections = document . editor . selections ;
127+
128+ for ( let selection of selections ) {
129+ if ( ! selection . isEmpty ) {
130+ g_styles . push ( document . getText ( selection ) ) ;
131+ }
132+ }
133+ } catch ( e ) {
134+ // g_output.appendLine(`NodeClient::updateStyleText, error=${e}`);
96135 }
97136
98- // 获取选中的内容
99- const selections = editor . selections ;
100- let styleSheets = [ ] ;
101- for ( let selection of selections ) {
102- if ( ! selection . isEmpty ) {
103- styleSheets . push ( document . getText ( selection ) ) ;
137+ try {
138+ if ( g_styles . length == 0 ) {
139+ g_styles . push ( document . getText ( ) ) ;
104140 }
141+ } catch ( e ) {
142+ // g_output.appendLine(`NodeClient::updateStyleText, error=${e}`);
105143 }
106- if ( styleSheets . length == 0 ) {
107- styleSheets . push ( document . getText ( ) ) ;
144+
145+ // g_output.appendLine(`NodeClient::updateStyleText, styles=${g_styles}`);
146+ }
147+
148+ /**
149+ * 更新自动应用定时器
150+ */
151+ function updateTimer ( ) {
152+ let isAuto = vscode . workspace . getConfiguration ( ) . get ( 'qsseditor.autoApply' , true ) ;
153+ if ( ! isAuto ) return ;
154+
155+ // 清除旧定时器
156+ if ( g_timerApply != undefined ) {
157+ clearTimeout ( g_timerApply ) ;
158+ g_timerApply = undefined ;
108159 }
109160
110- if ( styleSheets . length == 0 ) return ;
111- g_client . notify ( 'setStyleSheet' , styleSheets ) ;
161+ // 创建新的定时器
162+ g_timerApply = setTimeout ( function ( ) {
163+ onAutoApply ( ) ;
164+ g_timerApply = undefined ;
165+ } , 2000 ) ;
166+ }
167+
168+ /**
169+ * 选中控件
170+ * @param {string } word
171+ */
172+ function selectWidget ( word ) {
173+ if ( ! g_valid || g_client == undefined ) return ;
174+ g_client . notify ( 'selectWidget' , [ word ] ) ;
175+ } ;
176+
177+ /**
178+ * 应用样式
179+ */
180+ function applyStyle ( ) {
181+ // g_output.appendLine('NodeClient::applyStyle');
182+ if ( ! g_valid || g_client == undefined ) return ;
183+ updateStyleText ( undefined ) ;
184+ if ( g_styles . length == 0 ) return ;
185+ g_client . notify ( 'setStyleSheet' , g_styles ) ;
186+ g_styles = [ ] ;
112187} ;
113188
114189/**
@@ -130,49 +205,42 @@ function setPort() {
130205 if ( port == undefined ) return ;
131206 g_port = Number ( port ) ;
132207 vscode . workspace . getConfiguration ( ) . update ( 'qsseditor.serverPort' , g_port , true ) ;
133- console . log ( ' qsseditor.serverPort: ' + g_port ) ;
208+ g_output . appendLine ( ` qsseditor.serverPort: ${ g_port } ` ) ;
134209 if ( g_client != undefined ) g_client . close ( ) ; // 断开重连
135210 } ) ;
136211} ;
137212
138213
139214/**
140215 * 执行延迟自动应用样式
141- * @param {vscode.TextDocument } document
142- * @returns
143216 */
144- function onAutoApply ( document ) {
145- if ( ! g_autoApply ) return ;
146-
147- // 清除旧定时器
148- if ( g_timerApply != undefined ) {
149- clearTimeout ( g_timerApply ) ;
150- g_timerApply = undefined ;
151- }
152-
153- // 创建新的定时器
154- g_timerApply = setTimeout ( function ( ) {
155- // console.log('NodeClient::onAutoApply');
156- applyStyle ( document ) ;
157- g_timerApply = undefined ;
158- } , 2000 ) ;
159-
217+ function onAutoApply ( ) {
218+ // g_output.appendLine('NodeClient::onAutoApply');
219+ updateStyleText ( g_tmpDoc ) ;
220+ g_tmpDoc = undefined ;
221+ if ( g_styles . length == 0 ) return ;
222+ g_client . notify ( 'setStyleSheet' , g_styles ) ;
223+ g_styles = [ ] ;
160224} ;
161225
162226/**
163227 * 内容变化事件
164228 * @param {vscode.TextDocumentChangeEvent } event
165229 */
166230function onDidChangeTextDocument ( event ) {
167- onAutoApply ( event . document ) ;
231+ if ( ! g_valid || g_client == undefined ) return ;
232+ g_tmpDoc = event . document ;
233+ updateTimer ( ) ;
168234} ;
169235
170236/**
171237 * 保存事件
172238 * @param {vscode.TextDocument } document
173239 */
174240function onDidSaveTextDocument ( document ) {
175- onAutoApply ( document ) ;
241+ if ( ! g_valid || g_client == undefined ) return ;
242+ g_tmpDoc = document ;
243+ updateTimer ( ) ;
176244} ;
177245
178246/**
@@ -181,12 +249,14 @@ function onDidSaveTextDocument(document) {
181249 */
182250function setStatusBarCallback ( callback ) {
183251 g_updateStatusBar = callback ;
184- }
252+ } ;
185253
186254module . exports = {
187255 initKeywords,
188256 startClient,
189257 stopClient,
258+ setValid,
259+ setOutputChannel,
190260 selectWidget,
191261 applyStyle,
192262 setPort,
0 commit comments