-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
142 lines (125 loc) · 3.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
const {
LanguageClient,
SettingMonitor,
ExecuteCommandRequest,
DocumentFormattingRequest,
TextDocumentIdentifier,
} = require('vscode-languageclient');
const { workspace, commands: Commands, window: Window, languages: Languages } = require('vscode');
/**
* @typedef {import('vscode').ExtensionContext} ExtensionContext
*/
/**
* @param {ExtensionContext} context
*/
exports.activate = ({ subscriptions }) => {
const serverPath = require.resolve('./server.js');
const client = new LanguageClient(
'stylelint',
{
run: {
module: serverPath,
},
debug: {
module: serverPath,
options: {
execArgv: ['--nolazy', '--inspect=6004'],
},
},
},
{
documentSelector: [{ scheme: 'file' }, { scheme: 'untitled' }],
diagnosticCollectionName: 'stylelint',
synchronize: {
configurationSection: 'stylelint',
fileEvents: workspace.createFileSystemWatcher(
'**/{.stylelintrc{,.js,.json,.yaml,.yml},stylelint.config.js,.stylelintignore}',
),
},
},
);
client.onReady().then(() => {
/**
* Map of registered formatters by language ID.
* @type {Map<string, { dispose(): any }>}
*/
const registeredFormatters = new Map();
client.onNotification(
'stylelint/languageIdsAdded',
(/** @type {{langIds: string[]}} */ { langIds }) => {
for (const langId of langIds) {
// Avoid registering another formatter if we already registered one for the same language ID.
if (registeredFormatters.has(langId)) {
return;
}
const formatter = Languages.registerDocumentFormattingEditProvider(langId, {
provideDocumentFormattingEdits(textDocument, options) {
const params = {
textDocument: TextDocumentIdentifier.create(textDocument.uri.toString()),
options, // Editor formatting options, overriden by stylelint config.
};
// Request that the language server formats the document.
return client
.sendRequest(DocumentFormattingRequest.type, params)
.then(undefined, () => {
Window.showErrorMessage(
'Failed to format the document using stylelint. Please consider opening an issue with steps to reproduce.',
);
return null;
});
},
});
// Keep track of the new formatter.
registeredFormatters.set(langId, formatter);
}
},
);
client.onNotification(
'stylelint/languageIdsRemoved',
(/** @type {{langIds: string[]}} */ { langIds }) => {
for (const langId of langIds) {
const formatter = registeredFormatters.get(langId);
if (!formatter) {
return;
}
// Unregisters formatter.
formatter.dispose();
registeredFormatters.delete(langId);
}
},
);
// Make sure that formatters are disposed when extension is unloaded.
subscriptions.push({
dispose() {
for (const formatter of registeredFormatters.values()) {
formatter.dispose();
}
},
});
});
subscriptions.push(
Commands.registerCommand('stylelint.executeAutofix', async () => {
const textEditor = Window.activeTextEditor;
if (!textEditor) {
return;
}
const textDocument = {
uri: textEditor.document.uri.toString(),
version: textEditor.document.version,
};
const params = {
command: 'stylelint.applyAutoFix',
// https://github.com/microsoft/TypeScript/issues/43362
/* prettier-ignore */
'arguments': [textDocument],
};
await client.sendRequest(ExecuteCommandRequest.type, params).then(undefined, () => {
Window.showErrorMessage(
'Failed to apply stylelint fixes to the document. Please consider opening an issue with steps to reproduce.',
);
});
}),
);
subscriptions.push(new SettingMonitor(client, 'stylelint.enable').start());
};