Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pylsp/plugins/autopep8_format.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,23 +13,27 @@


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_document(config, document, options): # pylint: disable=unused-argument
log.info("Formatting document %s with autopep8", document)
return _format(config, document)
def pylsp_format_document(config, workspace, document, options): # pylint: disable=unused-argument
with workspace.report_progress("format: autopep8"):
log.info("Formatting document %s with autopep8", document)
return _format(config, document)


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_range(config, document, range, options): # pylint: disable=redefined-builtin,unused-argument
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)
def pylsp_format_range(
config, workspace, document, range, options
): # pylint: disable=redefined-builtin,unused-argument
with workspace.report_progress("format_range: autopep8"):
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)


def _format(config, document, line_range=None):
Expand Down
33 changes: 17 additions & 16 deletions pylsp/plugins/definition.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,24 +8,25 @@


@hookimpl
def pylsp_definitions(config, document, position):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)
def pylsp_definitions(config, workspace, document, position):
with workspace.report_progress("go to definitions"):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)

return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
}
}
for d in definitions if d.is_definition() and _not_internal_definition(d)
]
for d in definitions if d.is_definition() and _not_internal_definition(d)
]


def _not_internal_definition(definition):
Expand Down
103 changes: 52 additions & 51 deletions pylsp/plugins/flake8_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,57 +30,58 @@ def pylsp_settings():

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)
with workspace.report_progress("lint: flake8"):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)


def run_flake8(flake8_executable, args, document):
Expand Down
69 changes: 37 additions & 32 deletions pylsp/plugins/jedi_rename.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,39 +9,44 @@


@hookimpl
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []
for file_path, changed_file in refactoring.get_changed_files().items():
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument,too-many-locals
with workspace.report_progress("rename", percentage=0) as report_progress:
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
report_progress("refactoring")
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []

changed_files = refactoring.get_changed_files()
for n, (file_path, changed_file) in enumerate(changed_files.items()):
report_progress(changed_file, percentage=n/len(changed_files)*100)
Comment thread
syphar marked this conversation as resolved.
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
},
},
},
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}


def _num_lines(file_contents):
Expand Down
55 changes: 28 additions & 27 deletions pylsp/plugins/mccabe_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,30 +13,31 @@


@hookimpl
def pylsp_lint(config, document):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
def pylsp_lint(config, workspace, document):
with workspace.report_progress("lint: mccabe"):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
49 changes: 25 additions & 24 deletions pylsp/plugins/pycodestyle_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,30 +22,31 @@

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics
with workspace.report_progress("lint: pycodestyle"):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics


class PyCodeStyleDiagnosticReport(pycodestyle.BaseReport):
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pylsp/plugins/autopep8_format.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,23 +13,27 @@


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_document(config, document, options): # pylint: disable=unused-argument
log.info("Formatting document %s with autopep8", document)
return _format(config, document)
def pylsp_format_document(config, workspace, document, options): # pylint: disable=unused-argument
with workspace.report_progress("format: autopep8"):
log.info("Formatting document %s with autopep8", document)
return _format(config, document)


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_range(config, document, range, options): # pylint: disable=redefined-builtin,unused-argument
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)
def pylsp_format_range(
config, workspace, document, range, options
): # pylint: disable=redefined-builtin,unused-argument
with workspace.report_progress("format_range: autopep8"):
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)


def _format(config, document, line_range=None):
Expand Down
33 changes: 17 additions & 16 deletions pylsp/plugins/definition.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,24 +8,25 @@


@hookimpl
def pylsp_definitions(config, document, position):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)
def pylsp_definitions(config, workspace, document, position):
with workspace.report_progress("go to definitions"):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)

return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
}
}
for d in definitions if d.is_definition() and _not_internal_definition(d)
]
for d in definitions if d.is_definition() and _not_internal_definition(d)
]


def _not_internal_definition(definition):
Expand Down
103 changes: 52 additions & 51 deletions pylsp/plugins/flake8_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,57 +30,58 @@ def pylsp_settings():

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)
with workspace.report_progress("lint: flake8"):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)


def run_flake8(flake8_executable, args, document):
Expand Down
69 changes: 37 additions & 32 deletions pylsp/plugins/jedi_rename.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,39 +9,44 @@


@hookimpl
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []
for file_path, changed_file in refactoring.get_changed_files().items():
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument,too-many-locals
with workspace.report_progress("rename", percentage=0) as report_progress:
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
report_progress("refactoring")
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []

changed_files = refactoring.get_changed_files()
for n, (file_path, changed_file) in enumerate(changed_files.items()):
report_progress(changed_file, percentage=n/len(changed_files)*100)
Comment thread
syphar marked this conversation as resolved.
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
},
},
},
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}


def _num_lines(file_contents):
Expand Down
55 changes: 28 additions & 27 deletions pylsp/plugins/mccabe_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,30 +13,31 @@


@hookimpl
def pylsp_lint(config, document):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
def pylsp_lint(config, workspace, document):
with workspace.report_progress("lint: mccabe"):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
49 changes: 25 additions & 24 deletions pylsp/plugins/pycodestyle_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,30 +22,31 @@

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics
with workspace.report_progress("lint: pycodestyle"):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics


class PyCodeStyleDiagnosticReport(pycodestyle.BaseReport):
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pylsp/plugins/autopep8_format.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,23 +13,27 @@


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_document(config, document, options): # pylint: disable=unused-argument
log.info("Formatting document %s with autopep8", document)
return _format(config, document)
def pylsp_format_document(config, workspace, document, options): # pylint: disable=unused-argument
with workspace.report_progress("format: autopep8"):
log.info("Formatting document %s with autopep8", document)
return _format(config, document)


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_range(config, document, range, options): # pylint: disable=redefined-builtin,unused-argument
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)
def pylsp_format_range(
config, workspace, document, range, options
): # pylint: disable=redefined-builtin,unused-argument
with workspace.report_progress("format_range: autopep8"):
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)


def _format(config, document, line_range=None):
Expand Down
33 changes: 17 additions & 16 deletions pylsp/plugins/definition.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,24 +8,25 @@


@hookimpl
def pylsp_definitions(config, document, position):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)
def pylsp_definitions(config, workspace, document, position):
with workspace.report_progress("go to definitions"):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)

return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
}
}
for d in definitions if d.is_definition() and _not_internal_definition(d)
]
for d in definitions if d.is_definition() and _not_internal_definition(d)
]


def _not_internal_definition(definition):
Expand Down
103 changes: 52 additions & 51 deletions pylsp/plugins/flake8_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,57 +30,58 @@ def pylsp_settings():

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)
with workspace.report_progress("lint: flake8"):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)


def run_flake8(flake8_executable, args, document):
Expand Down
69 changes: 37 additions & 32 deletions pylsp/plugins/jedi_rename.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,39 +9,44 @@


@hookimpl
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []
for file_path, changed_file in refactoring.get_changed_files().items():
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument,too-many-locals
with workspace.report_progress("rename", percentage=0) as report_progress:
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
report_progress("refactoring")
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []

changed_files = refactoring.get_changed_files()
for n, (file_path, changed_file) in enumerate(changed_files.items()):
report_progress(changed_file, percentage=n/len(changed_files)*100)
Comment thread
syphar marked this conversation as resolved.
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
},
},
},
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}


def _num_lines(file_contents):
Expand Down
55 changes: 28 additions & 27 deletions pylsp/plugins/mccabe_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,30 +13,31 @@


@hookimpl
def pylsp_lint(config, document):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
def pylsp_lint(config, workspace, document):
with workspace.report_progress("lint: mccabe"):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
49 changes: 25 additions & 24 deletions pylsp/plugins/pycodestyle_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,30 +22,31 @@

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics
with workspace.report_progress("lint: pycodestyle"):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics


class PyCodeStyleDiagnosticReport(pycodestyle.BaseReport):
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pylsp/plugins/autopep8_format.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,23 +13,27 @@


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_document(config, document, options): # pylint: disable=unused-argument
log.info("Formatting document %s with autopep8", document)
return _format(config, document)
def pylsp_format_document(config, workspace, document, options): # pylint: disable=unused-argument
with workspace.report_progress("format: autopep8"):
log.info("Formatting document %s with autopep8", document)
return _format(config, document)


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_range(config, document, range, options): # pylint: disable=redefined-builtin,unused-argument
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)
def pylsp_format_range(
config, workspace, document, range, options
): # pylint: disable=redefined-builtin,unused-argument
with workspace.report_progress("format_range: autopep8"):
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)


def _format(config, document, line_range=None):
Expand Down
33 changes: 17 additions & 16 deletions pylsp/plugins/definition.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,24 +8,25 @@


@hookimpl
def pylsp_definitions(config, document, position):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)
def pylsp_definitions(config, workspace, document, position):
with workspace.report_progress("go to definitions"):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)

return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
}
}
for d in definitions if d.is_definition() and _not_internal_definition(d)
]
for d in definitions if d.is_definition() and _not_internal_definition(d)
]


def _not_internal_definition(definition):
Expand Down
103 changes: 52 additions & 51 deletions pylsp/plugins/flake8_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,57 +30,58 @@ def pylsp_settings():

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)
with workspace.report_progress("lint: flake8"):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)


def run_flake8(flake8_executable, args, document):
Expand Down
69 changes: 37 additions & 32 deletions pylsp/plugins/jedi_rename.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,39 +9,44 @@


@hookimpl
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []
for file_path, changed_file in refactoring.get_changed_files().items():
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument,too-many-locals
with workspace.report_progress("rename", percentage=0) as report_progress:
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
report_progress("refactoring")
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []

changed_files = refactoring.get_changed_files()
for n, (file_path, changed_file) in enumerate(changed_files.items()):
report_progress(changed_file, percentage=n/len(changed_files)*100)
Comment thread
syphar marked this conversation as resolved.
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
},
},
},
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}


def _num_lines(file_contents):
Expand Down
55 changes: 28 additions & 27 deletions pylsp/plugins/mccabe_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,30 +13,31 @@


@hookimpl
def pylsp_lint(config, document):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
def pylsp_lint(config, workspace, document):
with workspace.report_progress("lint: mccabe"):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
49 changes: 25 additions & 24 deletions pylsp/plugins/pycodestyle_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,30 +22,31 @@

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics
with workspace.report_progress("lint: pycodestyle"):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics


class PyCodeStyleDiagnosticReport(pycodestyle.BaseReport):
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pylsp/plugins/autopep8_format.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,23 +13,27 @@


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_document(config, document, options): # pylint: disable=unused-argument
log.info("Formatting document %s with autopep8", document)
return _format(config, document)
def pylsp_format_document(config, workspace, document, options): # pylint: disable=unused-argument
with workspace.report_progress("format: autopep8"):
log.info("Formatting document %s with autopep8", document)
return _format(config, document)


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_range(config, document, range, options): # pylint: disable=redefined-builtin,unused-argument
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)
def pylsp_format_range(
config, workspace, document, range, options
): # pylint: disable=redefined-builtin,unused-argument
with workspace.report_progress("format_range: autopep8"):
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)


def _format(config, document, line_range=None):
Expand Down
33 changes: 17 additions & 16 deletions pylsp/plugins/definition.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,24 +8,25 @@


@hookimpl
def pylsp_definitions(config, document, position):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)
def pylsp_definitions(config, workspace, document, position):
with workspace.report_progress("go to definitions"):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)

return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
}
}
for d in definitions if d.is_definition() and _not_internal_definition(d)
]
for d in definitions if d.is_definition() and _not_internal_definition(d)
]


def _not_internal_definition(definition):
Expand Down
103 changes: 52 additions & 51 deletions pylsp/plugins/flake8_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,57 +30,58 @@ def pylsp_settings():

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)
with workspace.report_progress("lint: flake8"):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)


def run_flake8(flake8_executable, args, document):
Expand Down
69 changes: 37 additions & 32 deletions pylsp/plugins/jedi_rename.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,39 +9,44 @@


@hookimpl
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []
for file_path, changed_file in refactoring.get_changed_files().items():
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument,too-many-locals
with workspace.report_progress("rename", percentage=0) as report_progress:
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
report_progress("refactoring")
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []

changed_files = refactoring.get_changed_files()
for n, (file_path, changed_file) in enumerate(changed_files.items()):
report_progress(changed_file, percentage=n/len(changed_files)*100)
Comment thread
syphar marked this conversation as resolved.
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
},
},
},
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}


def _num_lines(file_contents):
Expand Down
55 changes: 28 additions & 27 deletions pylsp/plugins/mccabe_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,30 +13,31 @@


@hookimpl
def pylsp_lint(config, document):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
def pylsp_lint(config, workspace, document):
with workspace.report_progress("lint: mccabe"):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
49 changes: 25 additions & 24 deletions pylsp/plugins/pycodestyle_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,30 +22,31 @@

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics
with workspace.report_progress("lint: pycodestyle"):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics


class PyCodeStyleDiagnosticReport(pycodestyle.BaseReport):
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pylsp/plugins/autopep8_format.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,23 +13,27 @@


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_document(config, document, options): # pylint: disable=unused-argument
log.info("Formatting document %s with autopep8", document)
return _format(config, document)
def pylsp_format_document(config, workspace, document, options): # pylint: disable=unused-argument
with workspace.report_progress("format: autopep8"):
log.info("Formatting document %s with autopep8", document)
return _format(config, document)


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_range(config, document, range, options): # pylint: disable=redefined-builtin,unused-argument
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)
def pylsp_format_range(
config, workspace, document, range, options
): # pylint: disable=redefined-builtin,unused-argument
with workspace.report_progress("format_range: autopep8"):
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)


def _format(config, document, line_range=None):
Expand Down
33 changes: 17 additions & 16 deletions pylsp/plugins/definition.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,24 +8,25 @@


@hookimpl
def pylsp_definitions(config, document, position):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)
def pylsp_definitions(config, workspace, document, position):
with workspace.report_progress("go to definitions"):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)

return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
}
}
for d in definitions if d.is_definition() and _not_internal_definition(d)
]
for d in definitions if d.is_definition() and _not_internal_definition(d)
]


def _not_internal_definition(definition):
Expand Down
103 changes: 52 additions & 51 deletions pylsp/plugins/flake8_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,57 +30,58 @@ def pylsp_settings():

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)
with workspace.report_progress("lint: flake8"):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)


def run_flake8(flake8_executable, args, document):
Expand Down
69 changes: 37 additions & 32 deletions pylsp/plugins/jedi_rename.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,39 +9,44 @@


@hookimpl
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []
for file_path, changed_file in refactoring.get_changed_files().items():
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument,too-many-locals
with workspace.report_progress("rename", percentage=0) as report_progress:
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
report_progress("refactoring")
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []

changed_files = refactoring.get_changed_files()
for n, (file_path, changed_file) in enumerate(changed_files.items()):
report_progress(changed_file, percentage=n/len(changed_files)*100)
Comment thread
syphar marked this conversation as resolved.
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
},
},
},
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}


def _num_lines(file_contents):
Expand Down
55 changes: 28 additions & 27 deletions pylsp/plugins/mccabe_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,30 +13,31 @@


@hookimpl
def pylsp_lint(config, document):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
def pylsp_lint(config, workspace, document):
with workspace.report_progress("lint: mccabe"):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
49 changes: 25 additions & 24 deletions pylsp/plugins/pycodestyle_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,30 +22,31 @@

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics
with workspace.report_progress("lint: pycodestyle"):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics


class PyCodeStyleDiagnosticReport(pycodestyle.BaseReport):
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pylsp/plugins/autopep8_format.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,23 +13,27 @@


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_document(config, document, options): # pylint: disable=unused-argument
log.info("Formatting document %s with autopep8", document)
return _format(config, document)
def pylsp_format_document(config, workspace, document, options): # pylint: disable=unused-argument
with workspace.report_progress("format: autopep8"):
log.info("Formatting document %s with autopep8", document)
return _format(config, document)


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_range(config, document, range, options): # pylint: disable=redefined-builtin,unused-argument
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)
def pylsp_format_range(
config, workspace, document, range, options
): # pylint: disable=redefined-builtin,unused-argument
with workspace.report_progress("format_range: autopep8"):
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)


def _format(config, document, line_range=None):
Expand Down
33 changes: 17 additions & 16 deletions pylsp/plugins/definition.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,24 +8,25 @@


@hookimpl
def pylsp_definitions(config, document, position):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)
def pylsp_definitions(config, workspace, document, position):
with workspace.report_progress("go to definitions"):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)

return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
}
}
for d in definitions if d.is_definition() and _not_internal_definition(d)
]
for d in definitions if d.is_definition() and _not_internal_definition(d)
]


def _not_internal_definition(definition):
Expand Down
103 changes: 52 additions & 51 deletions pylsp/plugins/flake8_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,57 +30,58 @@ def pylsp_settings():

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)
with workspace.report_progress("lint: flake8"):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)


def run_flake8(flake8_executable, args, document):
Expand Down
69 changes: 37 additions & 32 deletions pylsp/plugins/jedi_rename.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,39 +9,44 @@


@hookimpl
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []
for file_path, changed_file in refactoring.get_changed_files().items():
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument,too-many-locals
with workspace.report_progress("rename", percentage=0) as report_progress:
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
report_progress("refactoring")
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []

changed_files = refactoring.get_changed_files()
for n, (file_path, changed_file) in enumerate(changed_files.items()):
report_progress(changed_file, percentage=n/len(changed_files)*100)
Comment thread
syphar marked this conversation as resolved.
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
},
},
},
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}


def _num_lines(file_contents):
Expand Down
55 changes: 28 additions & 27 deletions pylsp/plugins/mccabe_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,30 +13,31 @@


@hookimpl
def pylsp_lint(config, document):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
def pylsp_lint(config, workspace, document):
with workspace.report_progress("lint: mccabe"):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
49 changes: 25 additions & 24 deletions pylsp/plugins/pycodestyle_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,30 +22,31 @@

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics
with workspace.report_progress("lint: pycodestyle"):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics


class PyCodeStyleDiagnosticReport(pycodestyle.BaseReport):
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions pylsp/plugins/autopep8_format.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,23 +13,27 @@


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_document(config, document, options): # pylint: disable=unused-argument
log.info("Formatting document %s with autopep8", document)
return _format(config, document)
def pylsp_format_document(config, workspace, document, options): # pylint: disable=unused-argument
with workspace.report_progress("format: autopep8"):
log.info("Formatting document %s with autopep8", document)
return _format(config, document)


@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
def pylsp_format_range(config, document, range, options): # pylint: disable=redefined-builtin,unused-argument
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)
def pylsp_format_range(
config, workspace, document, range, options
): # pylint: disable=redefined-builtin,unused-argument
with workspace.report_progress("format_range: autopep8"):
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# Add 1 for 1-indexing vs LSP's 0-indexing
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
return _format(config, document, line_range=line_range)


def _format(config, document, line_range=None):
Expand Down
33 changes: 17 additions & 16 deletions pylsp/plugins/definition.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,24 +8,25 @@


@hookimpl
def pylsp_definitions(config, document, position):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)
def pylsp_definitions(config, workspace, document, position):
with workspace.report_progress("go to definitions"):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)

return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
}
}
for d in definitions if d.is_definition() and _not_internal_definition(d)
]
for d in definitions if d.is_definition() and _not_internal_definition(d)
]


def _not_internal_definition(definition):
Expand Down
103 changes: 52 additions & 51 deletions pylsp/plugins/flake8_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,57 +30,58 @@ def pylsp_settings():

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)
with workspace.report_progress("lint: flake8"):
config = workspace._config
settings = config.plugin_settings('flake8', document_path=document.path)
log.debug("Got flake8 settings: %s", settings)

ignores = settings.get("ignore", [])
per_file_ignores = settings.get("perFileIgnores")

if per_file_ignores:
prev_file_pat = None
for path in per_file_ignores:
try:
file_pat, errors = path.split(":")
prev_file_pat = file_pat
except ValueError:
# It's legal to just specify another error type for the same
# file pattern:
if prev_file_pat is None:
log.warning(
"skipping a Per-file-ignore with no file pattern")
continue
file_pat = prev_file_pat
errors = path
if PurePath(document.path).match(file_pat):
ignores.extend(errors.split(","))

opts = {
'config': settings.get('config'),
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang-closing': settings.get('hangClosing'),
'ignore': ignores or None,
'max-complexity': settings.get('maxComplexity'),
'max-line-length': settings.get('maxLineLength'),
'indent-size': settings.get('indentSize'),
'select': settings.get('select'),
}

# flake takes only absolute path to the config. So we should check and
# convert if necessary
if opts.get('config') and not os.path.isabs(opts.get('config')):
opts['config'] = os.path.abspath(os.path.expanduser(os.path.expandvars(
opts.get('config')
)))
log.debug("using flake8 with config: %s", opts['config'])

# Call the flake8 utility then parse diagnostics from stdout
flake8_executable = settings.get('executable', 'flake8')

args = build_args(opts)
output = run_flake8(flake8_executable, args, document)
return parse_stdout(document, output)


def run_flake8(flake8_executable, args, document):
Expand Down
69 changes: 37 additions & 32 deletions pylsp/plugins/jedi_rename.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,39 +9,44 @@


@hookimpl
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []
for file_path, changed_file in refactoring.get_changed_files().items():
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument,too-many-locals
with workspace.report_progress("rename", percentage=0) as report_progress:
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
report_progress("refactoring")
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []

changed_files = refactoring.get_changed_files()
for n, (file_path, changed_file) in enumerate(changed_files.items()):
report_progress(changed_file, percentage=n/len(changed_files)*100)
Comment thread
syphar marked this conversation as resolved.
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
},
},
},
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}


def _num_lines(file_contents):
Expand Down
55 changes: 28 additions & 27 deletions pylsp/plugins/mccabe_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,30 +13,31 @@


@hookimpl
def pylsp_lint(config, document):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
def pylsp_lint(config, workspace, document):
with workspace.report_progress("lint: mccabe"):
threshold = config.plugin_settings('mccabe', document_path=document.path).get(THRESHOLD, DEFAULT_THRESHOLD)
log.debug("Running mccabe lint with threshold: %s", threshold)

try:
tree = compile(document.source, document.path, "exec", ast.PyCF_ONLY_AST)
except SyntaxError:
# We'll let the other linters point this one out
return None

visitor = mccabe.PathGraphingAstVisitor()
visitor.preorder(tree, visitor)

diags = []
for graph in visitor.graphs.values():
if graph.complexity() >= threshold:
diags.append({
'source': 'mccabe',
'range': {
'start': {'line': graph.lineno - 1, 'character': graph.column},
'end': {'line': graph.lineno - 1, 'character': len(document.lines[graph.lineno])},
},
'message': 'Cyclomatic complexity too high: %s (threshold %s)' % (graph.complexity(), threshold),
'severity': lsp.DiagnosticSeverity.Warning
})

return diags
49 changes: 25 additions & 24 deletions pylsp/plugins/pycodestyle_lint.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,30 +22,31 @@

@hookimpl
def pylsp_lint(workspace, document):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics
with workspace.report_progress("lint: pycodestyle"):
config = workspace._config
settings = config.plugin_settings('pycodestyle', document_path=document.path)
log.debug("Got pycodestyle settings: %s", settings)

opts = {
'exclude': settings.get('exclude'),
'filename': settings.get('filename'),
'hang_closing': settings.get('hangClosing'),
'ignore': settings.get('ignore'),
'max_line_length': settings.get('maxLineLength'),
'indent_size': settings.get('indentSize'),
'select': settings.get('select'),
}
kwargs = {k: v for k, v in opts.items() if v}
styleguide = pycodestyle.StyleGuide(kwargs)

c = pycodestyle.Checker(
filename=document.uri, lines=document.lines, options=styleguide.options,
report=PyCodeStyleDiagnosticReport(styleguide.options)
)
c.check_all()
diagnostics = c.report.diagnostics

return diagnostics


class PyCodeStyleDiagnosticReport(pycodestyle.BaseReport):
Expand Down
Loading