1
0
mirror of https://github.com/amix/vimrc synced 2025-07-01 20:55:00 +08:00

Updated plugins

This commit is contained in:
Amir
2023-04-01 22:48:04 +02:00
parent 2b653aa950
commit b318c1d0e5
96 changed files with 2382 additions and 625 deletions

View File

@ -157,7 +157,7 @@ function! ale#Queue(delay, ...) abort
endif
endfunction
let s:current_ale_version = [3, 2, 0]
let s:current_ale_version = [3, 3, 0]
" A function used to check for ALE features in files outside of the project.
function! ale#Has(feature) abort
@ -254,6 +254,7 @@ function! ale#GetLocItemMessage(item, format_string) abort
" \=l:variable is used to avoid escaping issues.
let l:msg = substitute(l:msg, '\v\%([^\%]*)code([^\%]*)\%', l:code_repl, 'g')
let l:msg = substitute(l:msg, '\V%severity%', '\=l:severity', 'g')
let l:msg = substitute(l:msg, '\V%type%', '\=l:type', 'g')
let l:msg = substitute(l:msg, '\V%linter%', '\=l:linter_name', 'g')
" Replace %s with the text.
let l:msg = substitute(l:msg, '\V%s', '\=a:item.text', 'g')

View File

@ -55,6 +55,7 @@ let s:global_variable_list = [
\ 'ale_sign_highlight_linenrs',
\ 'ale_statusline_format',
\ 'ale_type_map',
\ 'ale_use_neovim_diagnostics_api',
\ 'ale_use_global_executables',
\ 'ale_virtualtext_cursor',
\ 'ale_warn_about_trailing_blank_lines',

View File

@ -184,9 +184,13 @@ endfunction
function! ale#engine#SetResults(buffer, loclist) abort
let l:linting_is_done = !ale#engine#IsCheckingBuffer(a:buffer)
if g:ale_use_neovim_diagnostics_api
call ale#engine#SendResultsToNeovimDiagnostics(a:buffer, a:loclist)
endif
" Set signs first. This could potentially fix some line numbers.
" The List could be sorted again here by SetSigns.
if g:ale_set_signs
if !g:ale_use_neovim_diagnostics_api && g:ale_set_signs
call ale#sign#SetSigns(a:buffer, a:loclist)
endif
@ -199,11 +203,12 @@ function! ale#engine#SetResults(buffer, loclist) abort
call ale#statusline#Update(a:buffer, a:loclist)
endif
if g:ale_set_highlights
if !g:ale_use_neovim_diagnostics_api && g:ale_set_highlights
call ale#highlight#SetHighlights(a:buffer, a:loclist)
endif
if g:ale_virtualtext_cursor == 2
if !g:ale_use_neovim_diagnostics_api
\&& (g:ale_virtualtext_cursor is# 'all' || g:ale_virtualtext_cursor == 2)
call ale#virtualtext#SetTexts(a:buffer, a:loclist)
endif
@ -214,7 +219,8 @@ function! ale#engine#SetResults(buffer, loclist) abort
call ale#cursor#EchoCursorWarning()
endif
if g:ale_virtualtext_cursor == 1
if !g:ale_use_neovim_diagnostics_api
\&& (g:ale_virtualtext_cursor is# 'current' || g:ale_virtualtext_cursor == 1)
" Try and show the warning now.
" This will only do something meaningful if we're in normal mode.
call ale#virtualtext#ShowCursorWarning()
@ -238,6 +244,19 @@ function! ale#engine#SetResults(buffer, loclist) abort
endif
endfunction
function! ale#engine#SendResultsToNeovimDiagnostics(buffer, loclist) abort
if !has('nvim-0.6')
" We will warn the user on startup as well if they try to set
" g:ale_use_neovim_diagnostics_api outside of a Neovim context.
return
endif
" Keep the Lua surface area really small in the VimL part of ALE,
" and just require the diagnostics.lua module on demand.
let l:SendDiagnostics = luaeval('require("diagnostics").sendAleResultsToDiagnostics')
call l:SendDiagnostics(a:buffer, a:loclist)
endfunction
function! s:RemapItemTypes(type_map, loclist) abort
for l:item in a:loclist
let l:key = l:item.type

View File

@ -139,7 +139,7 @@ function! ale#events#Init() abort
autocmd InsertLeave * if exists('*ale#engine#Cleanup') | call ale#cursor#EchoCursorWarning() | endif
endif
if g:ale_virtualtext_cursor == 1
if g:ale_virtualtext_cursor is# 'current' || g:ale_virtualtext_cursor is# 1 || g:ale_virtualtext_cursor is# '1'
autocmd CursorMoved,CursorHold * if exists('*ale#engine#Cleanup') | call ale#virtualtext#ShowCursorWarningWithDelay() | endif
" Look for a warning to echo as soon as we leave Insert mode.
" The script's position variable used when moving the cursor will

View File

@ -7,6 +7,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['python'],
\ 'description': 'Add blank lines before control statements.',
\ },
\ 'alejandra': {
\ 'function': 'ale#fixers#alejandra#Fix',
\ 'suggested_filetypes': ['nix'],
\ 'description': 'The Uncompromising Nix Code Formatter',
\ },
\ 'align_help_tags': {
\ 'function': 'ale#fixers#help#AlignTags',
\ 'suggested_filetypes': ['help'],
@ -301,6 +306,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['gomod'],
\ 'description': 'Fix Go module files with go mod edit -fmt.',
\ },
\ 'gopls': {
\ 'function': 'ale#fixers#gopls#Fix',
\ 'suggested_filetypes': ['go'],
\ 'description': 'Fix Go files with gopls.',
\ },
\ 'tslint': {
\ 'function': 'ale#fixers#tslint#Fix',
\ 'suggested_filetypes': ['typescript'],
@ -580,6 +590,21 @@ let s:default_registry = {
\ 'function': 'ale#fixers#raco_fmt#Fix',
\ 'suggested_filetypes': ['racket'],
\ 'description': 'Fix Racket files with raco fmt.',
\ },
\ 'ruff': {
\ 'function': 'ale#fixers#ruff#Fix',
\ 'suggested_filetypes': ['python'],
\ 'description': 'Fix python files with ruff.',
\ },
\ 'pycln': {
\ 'function': 'ale#fixers#pycln#Fix',
\ 'suggested_filetypes': ['python'],
\ 'description': 'remove unused python import statements',
\ },
\ 'rustywind': {
\ 'function': 'ale#fixers#rustywind#Fix',
\ 'suggested_filetypes': ['html'],
\ 'description': 'Sort Tailwind CSS classes',
\ }
\}

View File

@ -0,0 +1,13 @@
call ale#Set('nix_alejandra_executable', 'alejandra')
call ale#Set('nix_alejandra_options', '')
function! ale#fixers#alejandra#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'nix_alejandra_executable')
let l:options = ale#Var(a:buffer, 'nix_alejandra_options')
return {
\ 'command': ale#Escape(l:executable)
\ . (empty(l:options) ? '' : ' ' . l:options)
\ . ' -- -'
\}
endfunction

View File

@ -0,0 +1,23 @@
" Author: Sean Enck <enckse@voidedtech.com>
" Description: Integration of gopls format with ALE.
call ale#Set('go_gopls_fix_executable', 'gopls')
call ale#Set('go_gopls_fix_options', '')
function! ale#fixers#gopls#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'go_gopls_fix_executable')
let l:options = ale#Var(a:buffer, 'go_gopls_fix_options')
let l:env = ale#go#EnvString(a:buffer)
if !executable(l:executable)
return 0
endif
return {
\ 'command': l:env . ale#Escape(l:executable)
\ . ' format'
\ . ale#Pad(l:options)
\ . ' -l -w %t',
\ 'read_temporary_file': 1,
\}
endfunction

View File

@ -0,0 +1,90 @@
" Author: Yining <zhang.yining@gmail.com>
" Description: pycln as ALE fixer for python files
call ale#Set('python_pycln_executable', 'pycln')
call ale#Set('python_pycln_options', '')
call ale#Set('python_pycln_use_global', get(g:, 'ale_use_global_executables', 0))
call ale#Set('python_pycln_change_directory', 1)
call ale#Set('python_pycln_auto_pipenv', 0)
call ale#Set('python_pycln_auto_poetry', 0)
call ale#Set('python_pycln_config_file', '')
function! ale#fixers#pycln#GetCwd(buffer) abort
if ale#Var(a:buffer, 'python_pycln_change_directory')
" Run from project root if found, else from buffer dir.
let l:project_root = ale#python#FindProjectRoot(a:buffer)
return !empty(l:project_root) ? l:project_root : '%s:h'
endif
return '%s:h'
endfunction
function! ale#fixers#pycln#GetExecutable(buffer) abort
if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_pycln_auto_pipenv'))
\ && ale#python#PipenvPresent(a:buffer)
return 'pipenv'
endif
if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_pycln_auto_poetry'))
\ && ale#python#PoetryPresent(a:buffer)
return 'poetry'
endif
return ale#python#FindExecutable(a:buffer, 'python_pycln', ['pycln'])
endfunction
function! ale#fixers#pycln#GetCommand(buffer) abort
let l:executable = ale#fixers#pycln#GetExecutable(a:buffer)
let l:exec_args = l:executable =~? 'pipenv\|poetry$'
\ ? ' run pycln'
\ : ''
return ale#Escape(l:executable) . l:exec_args
endfunction
function! ale#fixers#pycln#FixForVersion(buffer, version) abort
let l:executable = ale#fixers#pycln#GetExecutable(a:buffer)
let l:cmd = [ale#Escape(l:executable)]
if l:executable =~? 'pipenv\|poetry$'
call extend(l:cmd, ['run', 'pycln'])
endif
let l:options = ale#Var(a:buffer, 'python_pycln_options')
if !empty(l:options)
call add(l:cmd, l:options)
endif
let l:config_file = ale#Var(a:buffer, 'python_pycln_config_file')
let l:config_file = l:options !~# '\v(^| )--config ' && !empty(l:config_file)
\ ? ale#Escape(ale#path#Simplify(l:config_file))
\ : ''
if !empty(l:config_file)
call add(l:cmd, '--config ' . l:config_file)
endif
call add(l:cmd, '--silence')
" NOTE: pycln version `1.3.0` support reading from stdin
call add(l:cmd, ale#semver#GTE(a:version, [1, 3, 0]) ? '-' : '%s')
return {
\ 'cwd': ale#fixers#pycln#GetCwd(a:buffer),
\ 'command': join(l:cmd, ' '),
\}
endfunction
function! ale#fixers#pycln#Fix(buffer) abort
let l:executable = ale#fixers#pycln#GetExecutable(a:buffer)
let l:command = ale#fixers#pycln#GetCommand(a:buffer) . ale#Pad('--version')
return ale#semver#RunWithVersionCheck(
\ a:buffer,
\ l:executable,
\ l:command,
\ function('ale#fixers#pycln#FixForVersion'),
\)
endfunction

View File

@ -1,6 +1,13 @@
" Author: Yining <zhang.yining@gmail.com>
" Description: ruff as ALE fixer for python files
call ale#Set('python_ruff_executable', 'ruff')
call ale#Set('python_ruff_options', '')
call ale#Set('python_ruff_use_global', get(g:, 'ale_use_global_executables', 0))
call ale#Set('python_ruff_change_directory', 1)
call ale#Set('python_ruff_auto_pipenv', 0)
call ale#Set('python_ruff_auto_poetry', 0)
function! ale#fixers#ruff#GetCwd(buffer) abort
if ale#Var(a:buffer, 'python_ruff_change_directory')
" Run from project root if found, else from buffer dir.
@ -9,7 +16,7 @@ function! ale#fixers#ruff#GetCwd(buffer) abort
return !empty(l:project_root) ? l:project_root : '%s:h'
endif
return ''
return '%s:h'
endfunction
function! ale#fixers#ruff#GetExecutable(buffer) abort
@ -26,29 +33,57 @@ function! ale#fixers#ruff#GetExecutable(buffer) abort
return ale#python#FindExecutable(a:buffer, 'python_ruff', ['ruff'])
endfunction
function! ale#fixers#ruff#GetCommand(buffer, version) abort
let l:executable = ale_linters#python#ruff#GetExecutable(a:buffer)
function! ale#fixers#ruff#GetCommand(buffer) abort
let l:executable = ale#fixers#ruff#GetExecutable(a:buffer)
let l:exec_args = l:executable =~? 'pipenv\|poetry$'
\ ? ' run ruff'
\ : ''
" NOTE: ruff version `0.0.72` implement `--fix` with stdin
return ale#Escape(l:executable) . l:exec_args
\ . ale#Pad(ale#Var(a:buffer, 'python_ruff_options'))
\ . ' --fix'
\ . (ale#semver#GTE(a:version, [0, 0, 72]) ? ' -' : ' %s')
endfunction
function! ale#fixers#ruff#FixForVersion(buffer, version) abort
let l:executable = ale#fixers#ruff#GetExecutable(a:buffer)
let l:cmd = [ale#Escape(l:executable)]
if l:executable =~? 'pipenv\|poetry$'
call extend(l:cmd, ['run', 'ruff'])
endif
let l:options = ale#Var(a:buffer, 'python_ruff_options')
if !empty(l:options)
call add(l:cmd, l:options)
endif
" when --stdin-filename present, ruff will use it for proj root resolution
" https://github.com/charliermarsh/ruff/pull/1281
let l:fname = expand('#' . a:buffer . '...')
call add(l:cmd, '--stdin-filename '.ale#Escape(ale#path#Simplify(l:fname)))
call add(l:cmd, '--fix')
" NOTE: ruff version `0.0.72` implements `--fix` with stdin
if ale#semver#GTE(a:version, [0, 0, 72])
call add(l:cmd, '-')
else
call add(l:cmd, '%s')
endif
return {
\ 'cwd': ale#fixers#ruff#GetCwd(a:buffer),
\ 'command': join(l:cmd, ' '),
\}
endfunction
function! ale#fixers#ruff#Fix(buffer) abort
let l:fix_cmd = {buffer -> ale#semver#RunWithVersionCheck(
\ buffer,
\ ale#fixers#ruff#GetExecutable(buffer),
\ '%e --version',
\ function('ale#fixers#ruff#GetCommand'),
\ )}(a:buffer)
let l:executable = ale#fixers#ruff#GetExecutable(a:buffer)
let l:command = ale#fixers#ruff#GetCommand(a:buffer) . ale#Pad('--version')
return {
\ 'cwd': ale#fixers#ruff#GetCwd(a:buffer),
\ 'command': l:fix_cmd,
\}
return ale#semver#RunWithVersionCheck(
\ a:buffer,
\ l:executable,
\ l:command,
\ function('ale#fixers#ruff#FixForVersion'),
\)
endfunction

View File

@ -0,0 +1,17 @@
scriptencoding utf-8
" Author: Guillermo Roig <groig@protonmail.com>
" Description: Sort TailwindCSS classes with rustywind
call ale#Set('html_rustywind_executable', 'rustywind')
call ale#Set('html_rustywind_options', '')
function! ale#fixers#rustywind#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'html_rustywind_executable')
let l:options = ale#Var(a:buffer, 'html_rustywind_options')
return {
\ 'command': ale#Escape(l:executable)
\ . (empty(l:options) ? '' : ' ' . l:options)
\ . ' --stdin'
\}
endfunction

View File

@ -52,9 +52,9 @@ function! s:NvimShow(lines, options) abort
autocmd!
if g:ale_close_preview_on_insert
autocmd CursorMoved,TabLeave,WinLeave,InsertEnter <buffer> ++once call s:NvimClose()
autocmd CursorMoved,TabLeave,WinLeave,BufWinLeave,WinScrolled,InsertEnter <buffer> ++once call s:NvimClose()
else
autocmd CursorMoved,TabLeave,WinLeave <buffer> ++once call s:NvimClose()
autocmd CursorMoved,TabLeave,WinLeave,BufWinLeave,WinScrolled <buffer> ++once call s:NvimClose()
endif
augroup END
@ -99,48 +99,30 @@ function! s:NvimPrepareWindowContent(lines) abort
let l:width = max(map(copy(a:lines), 'strdisplaywidth(v:val)'))
let l:height = min([len(a:lines), l:max_height])
if empty(g:ale_floating_window_border)
return [a:lines, l:width, l:height]
endif
" Add the size of borders
let l:width += 2
let l:height += 2
let l:left = get(g:ale_floating_window_border, 0, '|')
let l:top = get(g:ale_floating_window_border, 1, '-')
let l:top_left = get(g:ale_floating_window_border, 2, '+')
let l:top_right = get(g:ale_floating_window_border, 3, '+')
let l:bottom_right = get(g:ale_floating_window_border, 4, '+')
let l:bottom_left = get(g:ale_floating_window_border, 5, '+')
let l:right = get(g:ale_floating_window_border, 6, l:left)
let l:bottom = get(g:ale_floating_window_border, 7, l:top)
let l:lines = [l:top_left . repeat(l:top, l:width - 2) . l:top_right]
for l:line in a:lines
let l:line_width = strchars(l:line)
let l:lines = add(l:lines, l:left . l:line . repeat(' ', l:width - l:line_width - 2). l:right)
endfor
" Truncate the lines
if len(l:lines) > l:max_height + 1
let l:lines = l:lines[0:l:max_height]
endif
let l:lines = add(l:lines, l:bottom_left . repeat(l:bottom, l:width - 2) . l:bottom_right)
return [l:lines, l:width, l:height]
return [a:lines[0:l:height-1], l:width, l:height]
endfunction
function! s:NvimCreate(options) abort
let l:left = get(g:ale_floating_window_border, 0, '|')
let l:top = get(g:ale_floating_window_border, 1, '-')
let l:popup_opts = extend({
\ 'relative': 'cursor',
\ 'row': 1,
\ 'col': 0,
\ 'width': 42,
\ 'height': 4,
\ 'style': 'minimal'
\ 'style': 'minimal',
\ 'border': empty(g:ale_floating_window_border) ? 'none' : [
\ get(g:ale_floating_window_border, 2, '+'),
\ l:top,
\ get(g:ale_floating_window_border, 3, '+'),
\ get(g:ale_floating_window_border, 6, l:left),
\ get(g:ale_floating_window_border, 4, '+'),
\ get(g:ale_floating_window_border, 7, l:top),
\ get(g:ale_floating_window_border, 5, '+'),
\ l:left,
\ ],
\ }, s:GetPopupOpts())
let l:buffer = nvim_create_buf(v:false, v:false)

View File

@ -0,0 +1,33 @@
function! ale#handlers#deadnix#Handle(buffer, lines) abort
let l:output = []
for l:line in a:lines
try
let l:file = ale#util#FuzzyJSONDecode(l:line, v:null)
catch
continue
endtry
if type(l:file) isnot v:t_dict
continue
endif
for l:error in l:file['results']
try
let l:ale_error = {
\ 'lnum': l:error['line'],
\ 'col': l:error['column'],
\ 'end_col': l:error['endColumn'],
\ 'text': l:error['message'],
\ 'type': 'W',
\}
catch
continue
endtry
call add(l:output, l:ale_error)
endfor
endfor
return l:output
endfunction

View File

@ -22,26 +22,6 @@ if !hlexists('ALEInfo')
highlight link ALEInfo ALEWarning
endif
if !hlexists('ALEVirtualTextError')
highlight link ALEVirtualTextError ALEError
endif
if !hlexists('ALEVirtualTextStyleError')
highlight link ALEVirtualTextStyleError ALEVirtualTextError
endif
if !hlexists('ALEVirtualTextWarning')
highlight link ALEVirtualTextWarning ALEWarning
endif
if !hlexists('ALEVirtualTextStyleWarning')
highlight link ALEVirtualTextStyleWarning ALEVirtualTextWarning
endif
if !hlexists('ALEVirtualTextInfo')
highlight link ALEVirtualTextInfo ALEVirtualTextWarning
endif
" The maximum number of items for the second argument of matchaddpos()
let s:MAX_POS_VALUES = 8
let s:MAX_COL_SIZE = 1073741824 " pow(2, 30)

View File

@ -51,7 +51,7 @@ let s:default_ale_linters = {
\ 'jsonc': [],
\ 'perl': ['perlcritic'],
\ 'perl6': [],
\ 'python': ['flake8', 'mypy', 'pylint', 'pyright'],
\ 'python': ['flake8', 'mypy', 'pylint', 'pyright', 'ruff'],
\ 'rust': ['cargo', 'rls'],
\ 'spec': [],
\ 'text': [],

View File

@ -52,28 +52,24 @@ function! ale#lsp#message#Exit() abort
endfunction
function! ale#lsp#message#DidOpen(buffer, language_id) abort
let l:lines = getbufline(a:buffer, 1, '$')
return [1, 'textDocument/didOpen', {
\ 'textDocument': {
\ 'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
\ 'languageId': a:language_id,
\ 'version': ale#lsp#message#GetNextVersionID(),
\ 'text': join(l:lines, "\n") . "\n",
\ 'text': ale#util#GetBufferContents(a:buffer),
\ },
\}]
endfunction
function! ale#lsp#message#DidChange(buffer) abort
let l:lines = getbufline(a:buffer, 1, '$')
" For changes, we simply send the full text of the document to the server.
return [1, 'textDocument/didChange', {
\ 'textDocument': {
\ 'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
\ 'version': ale#lsp#message#GetNextVersionID(),
\ },
\ 'contentChanges': [{'text': join(l:lines, "\n") . "\n"}]
\ 'contentChanges': [{'text': ale#util#GetBufferContents(a:buffer)}]
\}]
endfunction

View File

@ -0,0 +1,28 @@
" Author: w0rp <dev@w0rp.com>
" Description: Functions for integrating with Lua linters.
" Find project root for a Lua language server.
function! ale#lua#FindProjectRoot(buffer) abort
let l:possible_project_roots = [
\ '.git',
\ bufname(a:buffer),
\]
for l:possible_root in l:possible_project_roots
let l:project_root = ale#path#FindNearestFile(a:buffer, l:possible_root)
if empty(l:project_root)
let l:project_root = ale#path#FindNearestDirectory(a:buffer, l:possible_root)
endif
if !empty(l:project_root)
" dir:p expands to /full/path/to/dir/ whereas
" file:p expands to /full/path/to/file (no trailing slash)
" Appending '/' ensures that :h:h removes the path's last segment
" regardless of whether it is a directory or not.
return fnamemodify(l:project_root . '/', ':p:h:h')
endif
endfor
return ''
endfunction

View File

@ -1,4 +1,4 @@
" Author: w0rp <devw0rp@gmail.com>
" Author: w0rp <dev@w0rp.com>
" Description: Functions for integrating with Python linters.
call ale#Set('python_auto_pipenv', '0')
@ -96,6 +96,27 @@ function! ale#python#FindVirtualenv(buffer) abort
return $VIRTUAL_ENV
endfunction
" Automatically determine virtualenv environment variables and build
" a string of them to prefix linter commands with.
function! ale#python#AutoVirtualenvEnvString(buffer) abort
let l:venv_dir = ale#python#FindVirtualenv(a:buffer)
if !empty(l:venv_dir)
let l:strs = [ ]
" expand PATH correctly inside of the appropriate shell.
if has('win32')
call add(l:strs, 'set PATH=' . ale#Escape(l:venv_dir) . ';%PATH% && ')
else
call add(l:strs, 'PATH=' . ale#Escape(l:venv_dir) . '":$PATH" ')
endif
return join(l:strs, '')
endif
return ''
endfunction
" Given a buffer number and a command name, find the path to the executable.
" First search on a virtualenv for Python, if nothing is found, try the global
" command. Returns an empty string if cannot find the executable

View File

@ -14,7 +14,7 @@ function! s:DisablePostamble() abort
call ale#highlight#UpdateHighlights()
endif
if g:ale_virtualtext_cursor == 1
if g:ale_virtualtext_cursor isnot# 'disabled' && g:ale_virtualtext_cursor != 0
call ale#virtualtext#Clear(bufnr(''))
endif
endfunction

View File

@ -542,7 +542,7 @@ function! ale#util#SetBufferContents(buffer, lines) abort
endfunction
function! ale#util#GetBufferContents(buffer) abort
return join(getbufline(a:buffer, 1, '$'), '\n') . '\n'
return join(getbufline(a:buffer, 1, '$'), "\n") . "\n"
endfunction
function! ale#util#ToURI(resource) abort

View File

@ -3,88 +3,58 @@ scriptencoding utf-8
" Author: Luan Santos <cfcluan@gmail.com>
" Description: Shows lint message for the current line as virtualtext, if any
" Controls the milliseconds delay before showing a message.
let g:ale_virtualtext_delay = get(g:, 'ale_virtualtext_delay', 10)
let s:cursor_timer = -1
let s:last_pos = [0, 0, 0]
let s:has_virt_text = 0
let s:emulate_virt = 0
if has('nvim-0.3.2')
let s:ns_id = nvim_create_namespace('ale')
let s:has_virt_text = 1
elseif has('textprop') && has('popupwin')
let s:has_virt_text = 1
let s:emulate_virt = !has('patch-9.0.0297')
let s:hl_list = []
if s:emulate_virt
call prop_type_add('ale', {})
let s:last_virt = -1
endif
if !hlexists('ALEVirtualTextError')
highlight link ALEVirtualTextError Comment
endif
function! ale#virtualtext#Clear(buf) abort
if !s:has_virt_text
return
endif
if !hlexists('ALEVirtualTextStyleError')
highlight link ALEVirtualTextStyleError ALEVirtualTextError
endif
if has('nvim')
call nvim_buf_clear_namespace(a:buf, s:ns_id, 0, -1)
else
if s:emulate_virt && s:last_virt != -1
call prop_remove({'type': 'ale'})
call popup_close(s:last_virt)
let s:last_virt = -1
elseif !empty(s:hl_list)
call prop_remove({
\ 'types': s:hl_list,
\ 'all': 1,
\ 'bufnr': a:buf})
if !hlexists('ALEVirtualTextWarning')
highlight link ALEVirtualTextWarning Comment
endif
if !hlexists('ALEVirtualTextStyleWarning')
highlight link ALEVirtualTextStyleWarning ALEVirtualTextWarning
endif
if !hlexists('ALEVirtualTextInfo')
highlight link ALEVirtualTextInfo ALEVirtualTextWarning
endif
let g:ale_virtualtext_prefix =
\ get(g:, 'ale_virtualtext_prefix', '%comment% %type%: ')
" Controls the milliseconds delay before showing a message.
let g:ale_virtualtext_delay = get(g:, 'ale_virtualtext_delay', 10)
" Controls the positioning of virtualtext
let g:ale_virtualtext_column = get(g:, 'ale_virtualtext_column', 0)
let g:ale_virtualtext_maxcolumn = get(g:, 'ale_virtualtext_maxcolumn', 0)
let g:ale_virtualtext_single = get(g:,'ale_virtualtext_single',0)
let s:cursor_timer = get(s:, 'cursor_timer', -1)
let s:last_pos = get(s:, 'last_pos', [0, 0, 0])
let s:hl_list = get(s:, 'hl_list', [])
let s:last_message = ''
if !has_key(s:, 'has_virt_text')
let s:has_virt_text = 0
let s:emulate_virt = 0
let s:last_virt = -1
if has('nvim-0.3.2')
let s:ns_id = nvim_create_namespace('ale')
let s:has_virt_text = 1
elseif has('textprop') && has('popupwin')
let s:has_virt_text = 1
let s:emulate_virt = !has('patch-9.0.0297')
if s:emulate_virt
call prop_type_add('ale', {})
endif
endif
endfunction
function! ale#virtualtext#ShowMessage(message, hl_group, buf, line) abort
if !s:has_virt_text || !bufexists(str2nr(a:buf))
return
endif
let l:prefix = get(g:, 'ale_virtualtext_prefix', '> ')
let l:msg = l:prefix.trim(substitute(a:message, '\n', ' ', 'g'))
if has('nvim')
call nvim_buf_set_virtual_text(a:buf, s:ns_id, a:line-1, [[l:msg, a:hl_group]], {})
elseif s:emulate_virt
let l:left_pad = col('$')
call prop_add(a:line, l:left_pad, {
\ 'type': 'ale',
\})
let s:last_virt = popup_create(l:msg, {
\ 'line': -1,
\ 'padding': [0, 0, 0, 1],
\ 'mask': [[1, 1, 1, 1]],
\ 'textprop': 'ale',
\ 'highlight': a:hl_group,
\ 'fixed': 1,
\ 'wrap': 0,
\ 'zindex': 2
\})
else
let type = prop_type_get(a:hl_group)
if type == {}
call add(s:hl_list, a:hl_group)
call prop_type_add(a:hl_group, {'highlight': a:hl_group})
endif
call prop_add(a:line, 0, {
\ 'type': a:hl_group,
\ 'text': ' ' . l:msg,
\ 'bufnr': a:buf
\})
endif
endfunction
endif
function! s:StopCursorTimer() abort
if s:cursor_timer != -1
@ -93,65 +63,199 @@ function! s:StopCursorTimer() abort
endif
endfunction
function! ale#virtualtext#GetHlGroup(type, style) abort
if a:type is# 'E'
if a:style is# 'style'
return 'ALEVirtualTextStyleError'
else
return 'ALEVirtualTextError'
endif
elseif a:type is# 'W'
if a:style is# 'style'
return 'ALEVirtualTextStyleWarning'
else
return 'ALEVirtualTextWarning'
endif
function! ale#virtualtext#ResetDataForTests() abort
let s:last_pos = [0, 0, 0]
let s:last_message = ''
endfunction
function! ale#virtualtext#GetLastMessageForTests() abort
return s:last_message
endfunction
function! ale#virtualtext#GetComment(buffer) abort
let l:filetype = getbufvar(a:buffer, '&filetype')
let l:split = split(getbufvar(a:buffer, '&commentstring'), '%s')
return !empty(l:split) ? trim(l:split[0]) : '#'
endfunction
function! ale#virtualtext#Clear(buffer) abort
if !s:has_virt_text || !bufexists(str2nr(a:buffer))
return
endif
if has('nvim')
call nvim_buf_clear_namespace(a:buffer, s:ns_id, 0, -1)
else
return 'ALEVirtualTextInfo'
if s:emulate_virt && s:last_virt != -1
call prop_remove({'type': 'ale'})
call popup_close(s:last_virt)
let s:last_virt = -1
elseif !empty(s:hl_list)
call prop_remove({
\ 'types': s:hl_list,
\ 'all': 1,
\ 'bufnr': a:buffer,
\})
endif
endif
endfunction
function! ale#virtualtext#GetGroup(item) abort
let l:type = get(a:item, 'type', 'E')
let l:sub_type = get(a:item, 'sub_type', '')
if l:type is# 'E'
if l:sub_type is# 'style'
return 'ALEVirtualTextStyleError'
endif
return 'ALEVirtualTextError'
endif
if l:type is# 'W'
if l:sub_type is# 'style'
return 'ALEVirtualTextStyleWarning'
endif
return 'ALEVirtualTextWarning'
endif
return 'ALEVirtualTextInfo'
endfunction
function! ale#virtualtext#GetColumnPadding(buffer, line) abort
let l:mincol = ale#Var(a:buffer, 'virtualtext_column')
let l:maxcol = ale#Var(a:buffer, 'virtualtext_maxcolumn')
let l:win = bufwinnr(a:buffer)
if l:mincol[len(l:mincol)-1] is# '%'
let l:mincol = (winwidth(l:win) * l:mincol) / 100
endif
if l:maxcol[len(l:maxcol)-1] is# '%'
let l:maxcol = (winwidth(l:win) * l:maxcol) / 100
endif
" Calculate padding for virtualtext alignment
if l:mincol > 0 || l:maxcol > 0
let l:line_width = strdisplaywidth(getline(a:line))
if l:line_width < l:mincol
return l:mincol - l:line_width
elseif l:maxcol > 0 && l:line_width >= l:maxcol
" Stop processing if virtualtext would start beyond maxcol
return -1
endif
endif
" no padding.
return 0
endfunction
function! ale#virtualtext#ShowMessage(buffer, item) abort
if !s:has_virt_text || !bufexists(str2nr(a:buffer))
return
endif
let l:line = max([1, a:item.lnum])
let l:hl_group = ale#virtualtext#GetGroup(a:item)
" Get a language-appropriate comment character, or default to '#'.
let l:comment = ale#virtualtext#GetComment(a:buffer)
let l:prefix = ale#Var(a:buffer, 'virtualtext_prefix')
let l:prefix = ale#GetLocItemMessage(a:item, l:prefix)
let l:prefix = substitute(l:prefix, '\V%comment%', '\=l:comment', 'g')
let l:msg = l:prefix . substitute(a:item.text, '\n', ' ', 'g')
let l:col_pad = ale#virtualtext#GetColumnPadding(a:buffer, l:line)
" Store the last message we're going to set so we can read it in tests.
let s:last_message = l:msg
" Discard virtualtext if padding is negative.
if l:col_pad < 0
return
endif
if has('nvim')
call nvim_buf_set_virtual_text(
\ a:buffer,
\ s:ns_id, l:line - 1,
\ [[l:msg, l:hl_group]],
\ {}
\)
elseif s:emulate_virt
let l:left_pad = col('$')
call prop_add(l:line, l:left_pad, {'type': 'ale'})
let s:last_virt = popup_create(l:msg, {
\ 'line': -1,
\ 'padding': [0, 0, 0, 1],
\ 'mask': [[1, 1, 1, 1]],
\ 'textprop': 'ale',
\ 'highlight': l:hl_group,
\ 'fixed': 1,
\ 'wrap': 0,
\ 'zindex': 2
\})
else
let l:type = prop_type_get(l:hl_group)
if l:type == {}
call prop_type_add(l:hl_group, {'highlight': l:hl_group})
endif
" Add highlight groups to the list so we can clear them later.
if index(s:hl_list, l:hl_group) == -1
call add(s:hl_list, l:hl_group)
endif
" We ignore all errors from prop_add.
silent! call prop_add(l:line, 0, {
\ 'type': l:hl_group,
\ 'text': ' ' . l:msg,
\ 'bufnr': a:buffer,
\ 'text_padding_left': l:col_pad,
\})
endif
endfunction
function! ale#virtualtext#ShowCursorWarning(...) abort
if g:ale_virtualtext_cursor != 1
if g:ale_virtualtext_cursor isnot# 'current'
\&& g:ale_virtualtext_cursor != 1
return
endif
let l:buffer = bufnr('')
if mode(1) isnot# 'n'
\|| g:ale_use_neovim_diagnostics_api
\|| ale#ShouldDoNothing(l:buffer)
return
endif
if ale#ShouldDoNothing(l:buffer)
return
endif
let [l:info, l:loc] = ale#util#FindItemAtCursor(l:buffer)
let [l:info, l:item] = ale#util#FindItemAtCursor(l:buffer)
call ale#virtualtext#Clear(l:buffer)
if !empty(l:loc)
let l:msg = l:loc.text
let l:type = get(l:loc, 'type', 'E')
let l:style = get(l:loc, 'sub_type', '')
let l:hl_group = ale#virtualtext#GetHlGroup(l:type, l:style)
call ale#virtualtext#ShowMessage(l:msg, l:hl_group, l:buffer, line('.'))
if !empty(l:item)
call ale#virtualtext#ShowMessage(l:buffer, l:item)
endif
endfunction
function! ale#virtualtext#ShowCursorWarningWithDelay() abort
let l:buffer = bufnr('')
if g:ale_virtualtext_cursor != 1
return
endif
if mode(1) isnot# 'n'
if g:ale_virtualtext_cursor isnot# 'current'
\&& g:ale_virtualtext_cursor != 1
return
endif
call s:StopCursorTimer()
if mode(1) isnot# 'n'
\|| g:ale_use_neovim_diagnostics_api
return
endif
let l:pos = getpos('.')[0:2]
" Check the current buffer, line, and column number against the last
@ -169,19 +273,24 @@ function! ale#virtualtext#ShowCursorWarningWithDelay() abort
endif
endfunction
function! ale#virtualtext#SetTexts(buf, loclist) abort
function! ale#virtualtext#SetTexts(buffer, loclist) abort
if !has('nvim') && s:emulate_virt
return
endif
call ale#virtualtext#Clear(a:buf)
call ale#virtualtext#Clear(a:buffer)
for l in a:loclist
if l['bufnr'] != a:buf
continue
let l:filter = ale#Var(a:buffer,'virtualtext_single')
let l:seen = {}
for l:item in a:loclist
if l:item.bufnr == a:buffer
let l:line = max([1, l:item.lnum])
if !has_key(l:seen,l:line) || l:filter == 0
call ale#virtualtext#ShowMessage(a:buffer, l:item)
let l:seen[l:line] = 1
endif
endif
let hl = ale#virtualtext#GetHlGroup(l['type'], get(l, 'sub_type', ''))
call ale#virtualtext#ShowMessage(l['text'], hl, a:buf, l['lnum'])
endfor
endfunction