Skip to content

Commit 81620b7

Browse files
committed
✨ feat(apply): 优化日志输出和自动应用功能
1 parent b0fa5b6 commit 81620b7

File tree

3 files changed

+160
-72
lines changed

3 files changed

+160
-72
lines changed

src/client.js

Lines changed: 136 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ const keywords = require('./keywords.js');
44

55
var g_client;
66
var g_updateStatusBar;
7-
var g_timerConnect; // 重连定时器
87
var g_timerApply; // 延迟应用样式
9-
var g_autoApply = true; // 是否启用自动应用样式
108
var g_valid = true;
11-
var g_host = "localhost";
9+
var g_tmpDoc; // 临时文档
10+
var g_host = 'localhost';
1211
var 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
*/
1820
function initKeywords(context) {
21+
g_output.appendLine('NodeClient::initKeywords');
1922
keywords.register(context);
2023
};
2124

@@ -25,90 +28,162 @@ function initKeywords(context) {
2528
function 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
*/
3337
function 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
*/
6880
function 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
*/
166230
function 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
*/
174240
function 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
*/
182250
function setStatusBarCallback(callback) {
183251
g_updateStatusBar = callback;
184-
}
252+
};
185253

186254
module.exports = {
187255
initKeywords,
188256
startClient,
189257
stopClient,
258+
setValid,
259+
setOutputChannel,
190260
selectWidget,
191261
applyStyle,
192262
setPort,

src/extension.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ const vscode = require('vscode');
22
const client = require('./client.js');
33

44
var g_statusBar = null;
5+
var g_editor = null;
6+
var g_output = vscode.window.createOutputChannel('QSS Editor');
7+
8+
client.setOutputChannel(g_output);
59

610
/**
711
* 定义跳转
@@ -21,8 +25,9 @@ function provideDefinition(document, position, token) {
2125
* @param {vscode.ExtensionContext} context
2226
*/
2327
function activate(context) {
24-
console.log('extension "qsseditor" is now active!');
25-
console.log(process.env);
28+
g_output.show();
29+
g_output.clear();
30+
g_output.appendLine('extension "qsseditor" is now active!');
2631

2732
// 注册命令应用样式命令
2833
context.subscriptions.push(vscode.commands.registerCommand('qsseditor.applyStyle', function () {
@@ -48,9 +53,21 @@ function activate(context) {
4853
}));
4954

5055
// 注册事件
51-
vscode.workspace.onDidChangeTextDocument(client.onDidChangeTextDocument);
56+
vscode.window.onDidChangeActiveTextEditor(editor => {
57+
g_editor = editor;
58+
});
59+
vscode.workspace.onDidChangeTextDocument(event => {
60+
if (g_editor && event.document === g_editor.document) {
61+
if (event.document.languageId.toLowerCase() !== 'css' || !event.document.isDirty) {
62+
return;
63+
}
64+
client.onDidChangeTextDocument(event);
65+
}
66+
});
5267
vscode.workspace.onDidSaveTextDocument(client.onDidSaveTextDocument);
5368

69+
client.setValid(true);
70+
5471
// 注册状态栏
5572
if (!g_statusBar) {
5673
g_statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 0);
@@ -70,7 +87,7 @@ function activate(context) {
7087
function updateStatusBar(enabled) {
7188
if (g_statusBar) {
7289
g_statusBar.text = enabled ? `$(qss-status-on)` : `$(qss-status-off)`;
73-
g_statusBar.tooltip = enabled ? vscode.l10n.t("Connected") : vscode.l10n.t("Disconnected");
90+
g_statusBar.tooltip = enabled ? vscode.l10n.t('Connected') : vscode.l10n.t('Disconnected');
7491
g_statusBar.show();
7592
}
7693
}
@@ -79,7 +96,8 @@ function updateStatusBar(enabled) {
7996
* 插件被释放时触发
8097
*/
8198
function deactivate() {
82-
console.log('extension "qsseditor" is now deactivated!');
99+
g_output.appendLine('extension "qsseditor" is now deactivated!');
100+
client.setValid(false);
83101
client.stopClient();
84102
}
85103

src/keywords.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function register(context) {
1818
};
1919

2020
function add(name) {
21-
if (name == undefined || name.trim() == "" || g_words.has(name.trim())) return;
21+
if (name == undefined || name.trim() == '' || g_words.has(name.trim())) return;
2222
g_words.add(name.trim());
2323
g_items.push(new vscode.CompletionItem(name.trim(), vscode.CompletionItemKind.Keyword));
2424
};

0 commit comments

Comments
 (0)