diff --git a/LSP-eslint.sublime-settings b/LSP-eslint.sublime-settings index 10cd7bc..9f783a3 100644 --- a/LSP-eslint.sublime-settings +++ b/LSP-eslint.sublime-settings @@ -53,12 +53,26 @@ // The package manager you use to install node modules. // Possible values: `npm`, `yarn`, `pnpm` "packageManager": "npm", + // An array of language ids for which the plugin should probe if support is installed. + "probe": [ + "javascript", + "javascriptreact", + "typescript", + "typescriptreact", + "html", + "vue", + "markdown", + ], // Turns on quiet mode, which ignores warnings. "quiet": false, // Run the linter on save or on type. // Possible values: `onSave`, `onType` "run": "onType", - "validate": "on", + // An array of language ids which should be validated by ESLint. + "validate": [ + "javascript", + "javascriptreact", + ], // An optional list of working directories or a setting on how those are auto-resolved. // // This setting will normally only be set per-project to provide a list of working diff --git a/plugin.py b/plugin.py index abf0e26..97494cf 100644 --- a/plugin.py +++ b/plugin.py @@ -1,12 +1,12 @@ +from LSP.plugin.core.typing import Any, Dict, Literal, Set, Union +from LSP.plugin.core.url import uri_to_filename +from lsp_utils import NpmClientHandler import os import posixpath import re import sublime import webbrowser -from LSP.plugin.core.url import uri_to_filename -from lsp_utils import NpmClientHandler - def plugin_loaded(): LspEslintPlugin.setup() @@ -21,9 +21,14 @@ class LspEslintPlugin(NpmClientHandler): server_directory = 'language-server' server_binary_path = os.path.join(server_directory, 'out', 'eslintServer.js') + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self._probe_failed = set() # type: Set[str] + def on_ready(self, api) -> None: api.on_notification('eslint/status', self.handle_status) api.on_request('eslint/openDoc', self.handle_open_doc) + api.on_request('eslint/probeFailed', self.handle_probe_failed) def handle_status(self, params) -> None: pass @@ -32,6 +37,10 @@ def handle_open_doc(self, params, respond) -> None: webbrowser.open(params['url']) respond({}) + def handle_probe_failed(self, params, respond) -> None: + self._probe_failed.add(params['textDocument']['uri']) + respond(None) + def on_workspace_configuration(self, params, configuration) -> None: session = self.weaksession() if session: @@ -45,6 +54,12 @@ def on_workspace_configuration(self, params, configuration) -> None: if workspace_folder: configuration['workspaceFolder'] = workspace_folder.to_lsp() self.resolve_working_directory(configuration, scope_uri, workspace_folder) + buf = session.get_session_buffer_for_uri_async(scope_uri) + if buf: + configuration['validate'] = self.compute_validate(buf.language_id, scope_uri, configuration) + else: + configuration['validate'] = 'on' + del configuration['probe'] def resolve_working_directory(self, configuration, scope_uri, workspace_folder) -> None: working_directories = configuration.get('workingDirectories', None) @@ -105,3 +120,18 @@ def to_os_path(self, path) -> str: if sublime.platform == 'windows': path = re.sub(r'^\/(\w)\/', r'\1:\\', path) return os.path.normpath(path) + + def compute_validate(self, language_id: str, scope_uri: str, config: Dict[str, Any]) -> Literal['off', 'on', 'probe']: + validate = config.get('validate') + if isinstance(validate, list): + for validate_langugage_id in validate: + if validate_langugage_id == language_id: + return 'on' + if scope_uri in self._probe_failed: + return 'off' + probe = config.get('probe') + if isinstance(probe, list): + for probe_language_id in probe: + if probe_language_id == language_id: + return 'probe'; + return 'off'; diff --git a/sublime-package.json b/sublime-package.json index 6ac619b..fe2466a 100644 --- a/sublime-package.json +++ b/sublime-package.json @@ -73,6 +73,22 @@ "default": {}, "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)." }, + "probe": { + "type": "array", + "items": { + "type": "string" + }, + "default": [ + "javascript", + "javascriptreact", + "typescript", + "typescriptreact", + "html", + "vue", + "markdown" + ], + "description": "An array of language ids for which the plugin should probe if support is installed." + }, "run": { "type": "string", "enum": [ @@ -202,13 +218,15 @@ } }, "validate": { - "type": "string", - "enum": [ - "off", - "on", - "probe" + "type": "array", + "items": { + "type": "string" + }, + "default": [ + "javascript", + "javascriptreact" ], - "description": "Whether validation is enabled." + "description": "An array of language ids which should be validated by ESLint. If not installed ESLint will show an error." }, "codeAction.disableRuleComment": { "type": "object",