Skip to content

Commit

Permalink
Add support for "probe" setting
Browse files Browse the repository at this point in the history
Resolves #36
  • Loading branch information
rchl committed Oct 5, 2020
1 parent a998fdf commit be2f9ba
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
16 changes: 15 additions & 1 deletion LSP-eslint.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 33 additions & 3 deletions plugin.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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';
30 changes: 24 additions & 6 deletions sublime-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit be2f9ba

Please sign in to comment.