Skip to content

Commit 6ba00da

Browse files
committed
Add toggle to quickly toggle E501 + onDidChange
Changed nova.config.observe to nova.config.onDidChange to prevent triggers at extension load
1 parent 1b2f994 commit 6ba00da

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Version 1.1.0
22
- Automatically reload extension on preference changes.
33
- Minor bug fixes in Jedi workspace environment override.
4+
- Add quick toggle to enable and disable line length checks (E501) with PyCodeStyle.
45

56
## Version 1.0.3
67
- Add option to enable logging (Disabled by default)

Scripts/main.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ function showNotification(title, body) {
2525
}
2626
}
2727

28+
29+
// Append the argument to the array if the condition is true
30+
function conditionalAppendArgumentsToArray(conditional, array, arg) {
31+
// If the passed conditional is true
32+
if (conditional) {
33+
// If the array is empty return an array with just the passed argument.
34+
if (array == '') {
35+
return [arg];
36+
} else {
37+
// Otherwise append to the existing array and return.
38+
array.push(arg);
39+
return array;
40+
}
41+
} else {
42+
// If conditional is false, transparently return the passed array.
43+
return array;
44+
}
45+
}
46+
2847
// Parse JSON from a string and return an JSON object.
2948
function parseJSON(string) {
3049
if (string == undefined) {
@@ -124,7 +143,10 @@ function getSettings() {
124143
"exclude": parseSpaceSeparated(getPreference('pyls.plugins.pycodestyle.exclude')),
125144
"filename": parseSpaceSeparated(getPreference('pyls.plugins.pycodestyle.filename')),
126145
"select": parseSpaceSeparated(getPreference('pyls.plugins.pycodestyle.select')),
127-
"ignore": parseSpaceSeparated(getPreference('pyls.plugins.pycodestyle.ignore')),
146+
"ignore": conditionalAppendArgumentsToArray(
147+
getPreference('pyls.plugins.pycodestyle.disableLineLength'),
148+
parseSpaceSeparated(getPreference('pyls.plugins.pycodestyle.ignore')),
149+
"E501"),
128150
"hangClosing": getPreference('pyls.plugins.pycodestyle.hangClosing'),
129151
"maxLineLength": getPreference('pyls.plugins.pycodestyle.maxLineLength')
130152
},
@@ -181,6 +203,9 @@ function getSettings() {
181203
class PythonLanguageServer {
182204
constructor() {
183205
this.addPreferenceObservers();
206+
// First start.
207+
showNotification("Starting extension.");
208+
this.start(getPreference('pyls.executable', '/usr/local/bin/pyls'));
184209
}
185210

186211
addPreferenceObservers() {
@@ -217,6 +242,7 @@ class PythonLanguageServer {
217242
'pyls.plugins.pycodestyle.select',
218243
'pyls.plugins.pycodestyle.ignore',
219244
'pyls.plugins.pycodestyle.hangClosing',
245+
'pyls.plugins.pycodestyle.disableLineLength',
220246
'pyls.plugins.pycodestyle.maxLineLength',
221247
'pyls.plugins.pydocstyle.convention',
222248
'pyls.plugins.pydocstyle.addIgnore',
@@ -235,7 +261,7 @@ class PythonLanguageServer {
235261
'pyls.plugins.pyls_black.enabled'
236262
];
237263
for (var i of keys) {
238-
nova.config.observe(i, async function(newValue, oldValue) {
264+
nova.config.onDidChange(i, async function(newValue, oldValue) {
239265
console.log("Syncing preferences.");
240266
if(this.languageClient) {
241267
this.languageClient.sendNotification("workspace/didChangeConfiguration", getSettings())
@@ -249,18 +275,13 @@ class PythonLanguageServer {
249275
'pyls.logPath'
250276
];
251277
for (var i of reloadKeys) {
252-
nova.config.observe(i, async function(newValue, oldValue) {
278+
nova.config.onDidChange(i, async function(newValue, oldValue) {
253279
if(this.languageClient) {
254280
showNotification("Stopping extension.");
255-
await this.languageClient.stop();
256-
281+
await this.stop();
257282
nova.subscriptions.remove(this.languageClient);
258283
await this.start(getPreference('pyls.executable', '/usr/local/bin/pyls'));
259-
260-
nova.subscriptions.add(client);
261284
} else {
262-
// First start.
263-
showNotification("Starting extension.");
264285
await this.start(getPreference('pyls.executable', '/usr/local/bin/pyls'));
265286
}
266287
}, this);
@@ -270,7 +291,7 @@ class PythonLanguageServer {
270291
'pyls.plugins.jedi.workspace.environment'
271292
];
272293
for (var i of workspaceKeys) {
273-
nova.workspace.config.observe(i, async function(newValue, oldValue) {
294+
nova.workspace.config.onDidChange(i, async function(newValue, oldValue) {
274295
showNotification("Syncing Workspace Preferences.");
275296
if(this.languageClient) {
276297
this.languageClient.sendNotification("workspace/didChangeConfiguration", getSettings())
@@ -285,10 +306,7 @@ class PythonLanguageServer {
285306
}
286307

287308
async start(path) {
288-
if(this.languageClient) {
289-
await this.languageClient.stop();
290-
nova.subscriptions.remove(this.languageClient);
291-
}
309+
this.stop();
292310

293311
// Create the client
294312
var serverOptions = {
@@ -304,6 +322,7 @@ class PythonLanguageServer {
304322
// The set of document syntaxes for which the server is valid
305323
syntaxes: ['python'],
306324
};
325+
307326
var client = new LanguageClient('PyLS', 'Python Language Server', serverOptions, clientOptions);
308327

309328
try {

extension.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,13 @@
340340
"default": null,
341341
"description": "Hang closing bracket instead of matching indentation of opening bracket's line."
342342
},
343+
{
344+
"key": "pyls.plugins.pycodestyle.disableLineLength",
345+
"type": "boolean",
346+
"title": "Disable Maximum Line Length Check",
347+
"default": false,
348+
"description": "Shortcut to ignore the Maximum Line Length (E501) warning"
349+
},
343350
{
344351
"key": "pyls.plugins.pycodestyle.maxLineLength",
345352
"type": "number",

0 commit comments

Comments
 (0)