Skip to content

Commit be2f9ba

Browse files
committed
Add support for "probe" setting
Resolves sublimelsp#36
1 parent a998fdf commit be2f9ba

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

LSP-eslint.sublime-settings

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,26 @@
5353
// The package manager you use to install node modules.
5454
// Possible values: `npm`, `yarn`, `pnpm`
5555
"packageManager": "npm",
56+
// An array of language ids for which the plugin should probe if support is installed.
57+
"probe": [
58+
"javascript",
59+
"javascriptreact",
60+
"typescript",
61+
"typescriptreact",
62+
"html",
63+
"vue",
64+
"markdown",
65+
],
5666
// Turns on quiet mode, which ignores warnings.
5767
"quiet": false,
5868
// Run the linter on save or on type.
5969
// Possible values: `onSave`, `onType`
6070
"run": "onType",
61-
"validate": "on",
71+
// An array of language ids which should be validated by ESLint.
72+
"validate": [
73+
"javascript",
74+
"javascriptreact",
75+
],
6276
// An optional list of working directories or a setting on how those are auto-resolved.
6377
//
6478
// This setting will normally only be set per-project to provide a list of working

plugin.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
from LSP.plugin.core.typing import Any, Dict, Literal, Set, Union
2+
from LSP.plugin.core.url import uri_to_filename
3+
from lsp_utils import NpmClientHandler
14
import os
25
import posixpath
36
import re
47
import sublime
58
import webbrowser
69

7-
from LSP.plugin.core.url import uri_to_filename
8-
from lsp_utils import NpmClientHandler
9-
1010

1111
def plugin_loaded():
1212
LspEslintPlugin.setup()
@@ -21,9 +21,14 @@ class LspEslintPlugin(NpmClientHandler):
2121
server_directory = 'language-server'
2222
server_binary_path = os.path.join(server_directory, 'out', 'eslintServer.js')
2323

24+
def __init__(self, *args: Any, **kwargs: Any) -> None:
25+
super().__init__(*args, **kwargs)
26+
self._probe_failed = set() # type: Set[str]
27+
2428
def on_ready(self, api) -> None:
2529
api.on_notification('eslint/status', self.handle_status)
2630
api.on_request('eslint/openDoc', self.handle_open_doc)
31+
api.on_request('eslint/probeFailed', self.handle_probe_failed)
2732

2833
def handle_status(self, params) -> None:
2934
pass
@@ -32,6 +37,10 @@ def handle_open_doc(self, params, respond) -> None:
3237
webbrowser.open(params['url'])
3338
respond({})
3439

40+
def handle_probe_failed(self, params, respond) -> None:
41+
self._probe_failed.add(params['textDocument']['uri'])
42+
respond(None)
43+
3544
def on_workspace_configuration(self, params, configuration) -> None:
3645
session = self.weaksession()
3746
if session:
@@ -45,6 +54,12 @@ def on_workspace_configuration(self, params, configuration) -> None:
4554
if workspace_folder:
4655
configuration['workspaceFolder'] = workspace_folder.to_lsp()
4756
self.resolve_working_directory(configuration, scope_uri, workspace_folder)
57+
buf = session.get_session_buffer_for_uri_async(scope_uri)
58+
if buf:
59+
configuration['validate'] = self.compute_validate(buf.language_id, scope_uri, configuration)
60+
else:
61+
configuration['validate'] = 'on'
62+
del configuration['probe']
4863

4964
def resolve_working_directory(self, configuration, scope_uri, workspace_folder) -> None:
5065
working_directories = configuration.get('workingDirectories', None)
@@ -105,3 +120,18 @@ def to_os_path(self, path) -> str:
105120
if sublime.platform == 'windows':
106121
path = re.sub(r'^\/(\w)\/', r'\1:\\', path)
107122
return os.path.normpath(path)
123+
124+
def compute_validate(self, language_id: str, scope_uri: str, config: Dict[str, Any]) -> Literal['off', 'on', 'probe']:
125+
validate = config.get('validate')
126+
if isinstance(validate, list):
127+
for validate_langugage_id in validate:
128+
if validate_langugage_id == language_id:
129+
return 'on'
130+
if scope_uri in self._probe_failed:
131+
return 'off'
132+
probe = config.get('probe')
133+
if isinstance(probe, list):
134+
for probe_language_id in probe:
135+
if probe_language_id == language_id:
136+
return 'probe';
137+
return 'off';

sublime-package.json

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@
7373
"default": {},
7474
"markdownDescription": "The eslint options object to provide args normally passed to eslint when executed from a command line (see http://eslint.org/docs/developer-guide/nodejs-api#cliengine)."
7575
},
76+
"probe": {
77+
"type": "array",
78+
"items": {
79+
"type": "string"
80+
},
81+
"default": [
82+
"javascript",
83+
"javascriptreact",
84+
"typescript",
85+
"typescriptreact",
86+
"html",
87+
"vue",
88+
"markdown"
89+
],
90+
"description": "An array of language ids for which the plugin should probe if support is installed."
91+
},
7692
"run": {
7793
"type": "string",
7894
"enum": [
@@ -202,13 +218,15 @@
202218
}
203219
},
204220
"validate": {
205-
"type": "string",
206-
"enum": [
207-
"off",
208-
"on",
209-
"probe"
221+
"type": "array",
222+
"items": {
223+
"type": "string"
224+
},
225+
"default": [
226+
"javascript",
227+
"javascriptreact"
210228
],
211-
"description": "Whether validation is enabled."
229+
"description": "An array of language ids which should be validated by ESLint. If not installed ESLint will show an error."
212230
},
213231
"codeAction.disableRuleComment": {
214232
"type": "object",

0 commit comments

Comments
 (0)