or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/coc.nvim@0.0.x

docs

index.md
tile.json

tessl/npm-coc-nvim

tessl install tessl/npm-coc-nvim@0.0.0

LSP based intellisense engine for neovim & vim8.

code-actions.mddocs/intelligence/

Code Actions and Refactoring [Basic]

Complexity: Basic | Category: Intelligence

Common Tasks: Apply quick fixes | Rename symbols | Organize imports | Refactor code

Apply code actions, quick fixes, refactorings, and symbol renaming provided by language servers.

Table of Contents

  • Common Tasks
  • Key Mappings
  • CocAction API
  • Configuration
  • Highlight Groups
  • Usage Examples
  • Error Handling
  • Troubleshooting
  • See Also

Common Tasks

Apply Quick Fix

nmap <leader>qf <Plug>(coc-fix-current)

Apply first available quick fix for current line.

Show Code Actions

nmap <leader>ac <Plug>(coc-codeaction-cursor)

Show available code actions for cursor position.

Rename Symbol

nmap <leader>rn <Plug>(coc-rename)

Rename symbol at cursor across workspace.

Organize Imports

:call CocAction('organizeImport')

Organize and sort imports in current file.

Key Mappings

<Plug>(coc-codeaction)

<Plug>(coc-codeaction)

Show code actions available for the current file or buffer.

Example:

nmap <leader>a <Plug>(coc-codeaction)

<Plug>(coc-codeaction-line)

<Plug>(coc-codeaction-line)

Show code actions for the current line.

Example:

nmap <leader>al <Plug>(coc-codeaction-line)

<Plug>(coc-codeaction-cursor)

<Plug>(coc-codeaction-cursor)

Show code actions for cursor position only.

Example:

nmap <leader>ac <Plug>(coc-codeaction-cursor)

<Plug>(coc-codeaction-selected)

<Plug>(coc-codeaction-selected)

Code action for selected range. Works as operator in normal mode or with visual selection.

Example:

nmap <leader>a <Plug>(coc-codeaction-selected)
xmap <leader>a <Plug>(coc-codeaction-selected)

<Plug>(coc-fix-current)

<Plug>(coc-fix-current)

Apply first available quick fix for current line.

Example:

nmap <leader>qf <Plug>(coc-fix-current)

<Plug>(coc-rename)

<Plug>(coc-rename)

Rename symbol at cursor position across workspace.

Example:

nmap <leader>rn <Plug>(coc-rename)

<Plug>(coc-refactor)

<Plug>(coc-refactor)

Open refactor window for advanced refactoring operations.

Example:

nmap <leader>rf <Plug>(coc-refactor)

<Plug>(coc-codelens-action)

<Plug>(coc-codelens-action)

Invoke code lens action at cursor position.

Example:

nmap <leader>cl <Plug>(coc-codelens-action)

CocAction API

codeAction

CocAction('codeAction', [mode], [only])

Get and apply code actions.

Parameters:

  • mode: Scope mode - '' (buffer), 'line', 'cursor', or visual mode ('v', 'V', 'x')
  • only: Optional array of CodeActionKind to filter (e.g., ['quickfix'], ['refactor'])

Example:

" All buffer actions
call CocAction('codeAction', '')

" Line actions
call CocAction('codeAction', 'line')

" Cursor actions
call CocAction('codeAction', 'cursor')

" Only quickfix actions
call CocAction('codeAction', 'cursor', ['quickfix'])

" Only refactor actions
call CocAction('codeAction', 'line', ['refactor'])

codeActions

CocAction('codeActions', [mode], [only])
" Returns: list

Get available code actions without applying.

Parameters:

  • mode: Scope mode
  • only: Optional filter by CodeActionKind

Returns: List of code action objects.

Example:

let actions = CocAction('codeActions', 'cursor')
echo 'Available actions: ' . len(actions)

for action in actions
  echo action.title
endfor

codeActionRange

CocAction('codeActionRange', start, end, [only])

Get code actions for specific line range.

Parameters:

  • start: Start line number (0-based)
  • end: End line number (0-based)
  • only: Optional CodeActionKind filter

Example:

" Actions for lines 10-20
call CocAction('codeActionRange', 9, 19)

" Only refactor actions for range
call CocAction('codeActionRange', 9, 19, ['refactor'])

doCodeAction

CocAction('doCodeAction', action)

Apply a specific code action object.

Parameters:

  • action: Code action object from codeActions()

Example:

let actions = CocAction('codeActions', 'cursor')
if len(actions) > 0
  call CocAction('doCodeAction', actions[0])
endif

doQuickfix

CocAction('doQuickfix')

Apply first available quickfix action for current line.

Example:

nnoremap <silent> <leader>qf :call CocAction('doQuickfix')<CR>

quickfixes

CocAction('quickfixes', [mode])
" Returns: list

Get available quickfix actions.

Parameters:

  • mode: Optional scope mode

Returns: List of quickfix action objects.

Example:

let fixes = CocAction('quickfixes', 'cursor')
echo len(fixes) . ' quickfixes available'

organizeImport

CocAction('organizeImport')

Organize and sort imports for current file.

Example:

nnoremap <silent> <leader>oi :call CocAction('organizeImport')<CR>

" Auto-organize on save
autocmd BufWritePre *.ts,*.tsx call CocAction('organizeImport')

fixAll

CocAction('fixAll')

Apply all available auto-fixes for current buffer.

Example:

nnoremap <silent> <leader>fa :call CocAction('fixAll')<CR>

rename

CocAction('rename')

Trigger rename operation for symbol at cursor.

Example:

nnoremap <silent> <leader>rn :call CocAction('rename')<CR>

refactor

CocAction('refactor')

Open refactor window showing available refactoring operations.

Example:

nnoremap <silent> <leader>re :call CocAction('refactor')<CR>

codeLensAction

CocAction('codeLensAction')

Execute code lens action at cursor position.

Example:

nnoremap <silent> <leader>cl :call CocAction('codeLensAction')<CR>

Configuration

Code action settings in coc-settings.json:

{
  "coc.preferences.enableCodeActions": true,
  "codeAction.disabledSupport": false,
  "codeAction.showDocumentation": true,
  "codeLens.enable": true,
  "codeLens.separator": "‣"
}

Highlight Groups

CocCodeLens

CocCodeLens

Highlight group for code lens virtual text.

Example:

highlight CocCodeLens ctermfg=Gray guifg=#999999

Usage Examples

Complete Code Action Setup

" Code actions
nmap <leader>ac <Plug>(coc-codeaction-cursor)
nmap <leader>al <Plug>(coc-codeaction-line)
nmap <leader>as <Plug>(coc-codeaction-selected)
xmap <leader>a  <Plug>(coc-codeaction-selected)

" Quick fix
nmap <leader>qf <Plug>(coc-fix-current)

" Rename
nmap <leader>rn <Plug>(coc-rename)

" Refactor
nmap <leader>rf <Plug>(coc-refactor)

" Code lens
nmap <leader>cl <Plug>(coc-codelens-action)

Auto-Fix on Save

" Apply quickfix on save
function! s:cocAutoFix() abort
  call CocAction('doQuickfix')
endfunction

autocmd BufWritePre *.ts,*.tsx,*.js,*.jsx call s:cocAutoFix()

" Organize imports on save
autocmd BufWritePre *.ts,*.tsx,*.go call CocAction('organizeImport')

" Fix all on save
autocmd BufWritePre *.ts,*.tsx call CocAction('fixAll')

Interactive Code Actions

" Show code actions with preview
function! ShowCodeActions() abort
  let actions = CocAction('codeActions', 'cursor')

  if len(actions) == 0
    echo "No code actions available"
    return
  endif

  " Build menu items
  let items = []
  for i in range(len(actions))
    call add(items, (i + 1) . '. ' . actions[i].title)
  endfor

  " Show menu and apply selected action
  let choice = inputlist(['Select action:'] + items)
  if choice > 0 && choice <= len(actions)
    call CocAction('doCodeAction', actions[choice - 1])
  endif
endfunction

nnoremap <silent> <leader>ca :call ShowCodeActions()<CR>

Filter Actions by Kind

" Show only quickfix actions
function! ShowQuickFixes() abort
  let fixes = CocAction('quickfixes', 'cursor')

  if len(fixes) == 0
    echo "No quickfixes available"
    return
  endif

  echo len(fixes) . " quickfix(es) available"
  call CocAction('codeAction', 'cursor', ['quickfix'])
endfunction

nnoremap <silent> <leader>qx :call ShowQuickFixes()<CR>

" Show only refactor actions
nnoremap <silent> <leader>rr :call CocAction('codeAction', 'cursor', ['refactor'])<CR>

Smart Rename

" Rename with confirmation
function! SmartRename() abort
  let current = expand('<cword>')
  let new_name = input('Rename "' . current . '" to: ')

  if empty(new_name) || new_name == current
    echo " Cancelled"
    return
  endif

  call CocAction('rename')
endfunction

nnoremap <silent> <leader>rn :call SmartRename()<CR>

Organize Imports with Feedback

function! OrganizeImports() abort
  echo "Organizing imports..."
  call CocAction('organizeImport')
  echo "Imports organized"
endfunction

nnoremap <silent> <leader>oi :call OrganizeImports()<CR>

Code Action Preview

" Preview what actions are available
function! PreviewCodeActions() abort
  let actions = CocAction('codeActions', 'line')

  echo "=== Code Actions ==="
  if len(actions) == 0
    echo "No actions available"
    return
  endif

  for action in actions
    echo "• " . action.title
    if has_key(action, 'kind')
      echo "  Kind: " . action.kind
    endif
  endfor
endfunction

nnoremap <silent> <leader>ap :call PreviewCodeActions()<CR>

Auto-Fix All

" Fix all issues in file
function! FixAllIssues() abort
  echo "Applying all fixes..."

  " Apply quickfixes
  call CocAction('fixAll')

  " Organize imports
  call CocAction('organizeImport')

  " Format
  call CocAction('format')

  echo "All fixes applied"
endfunction

command! FixAll call FixAllIssues()
nnoremap <silent> <leader>fix :FixAll<CR>

Refactor Menu

function! RefactorMenu() abort
  let actions = CocAction('codeActions', 'cursor', ['refactor'])

  if len(actions) == 0
    echo "No refactoring actions available"
    return
  endif

  let menu = ['Available refactorings:']
  for i in range(len(actions))
    call add(menu, (i + 1) . '. ' . actions[i].title)
  endfor

  let choice = inputlist(menu)
  if choice > 0 && choice <= len(actions)
    call CocAction('doCodeAction', actions[choice - 1])
    echo "Applied: " . actions[choice - 1].title
  endif
endfunction

nnoremap <silent> <leader>ref :call RefactorMenu()<CR>

Code Lens Integration

" Auto-update code lens
autocmd CursorHold,CursorHoldI * silent call CocActionAsync('codeLensAction')

" Toggle code lens
function! ToggleCodeLens() abort
  if get(g:, 'coc_codelens_enable', 1)
    let g:coc_codelens_enable = 0
    echo "Code lens disabled"
  else
    let g:coc_codelens_enable = 1
    echo "Code lens enabled"
  endif
endfunction

nnoremap <silent> <leader>tl :call ToggleCodeLens()<CR>

Range-Based Actions

" Apply code action to function
function! CodeActionForFunction() abort
  " Get function range
  normal! [m
  let start = line('.')
  normal! ]M
  let end = line('.')

  call CocAction('codeActionRange', start - 1, end - 1)
endfunction

nnoremap <silent> <leader>af :call CodeActionForFunction()<CR>

" Apply to visual selection
xnoremap <silent> <leader>a :call CocAction('codeAction', visualmode())<CR>

Quick Fix with Count

" Apply multiple quickfixes
function! QuickFixMultiple(count) abort
  for i in range(a:count)
    call CocAction('doQuickfix')
  endfor
  echo a:count . " quickfixes applied"
endfunction

nnoremap <silent> <leader>qf :<C-u>call QuickFixMultiple(v:count1)<CR>

Source Actions

" Source-specific actions
function! SourceActions() abort
  " Source actions are typically file-level
  let actions = CocAction('codeActions', '', ['source'])

  if len(actions) == 0
    echo "No source actions available"
    return
  endif

  for action in actions
    echo "• " . action.title
  endfor

  call CocAction('codeAction', '', ['source'])
endfunction

nnoremap <silent> <leader>sa :call SourceActions()<CR>

Action Statistics

function! CodeActionStats() abort
  let all_actions = CocAction('codeActions', '')
  let quickfixes = CocAction('codeActions', '', ['quickfix'])
  let refactors = CocAction('codeActions', '', ['refactor'])

  echo "=== Code Action Statistics ==="
  echo "Total actions: " . len(all_actions)
  echo "Quick fixes: " . len(quickfixes)
  echo "Refactorings: " . len(refactors)
endfunction

command! CodeActionStats call CodeActionStats()

Error Handling

No Code Actions Available

Problem: Code action command shows no available actions.

Solutions:

" Check if language server supports code actions
if CocHasProvider('codeAction')
  echo "Code actions supported"
else
  echo "No code action provider"
endif

" Verify cursor is on valid symbol
echo expand('<cword>')

" Check language server status
:CocInfo

Rename Failed

Problem: Symbol rename operation fails or doesn't affect all occurrences.

Solutions:

" Ensure file is saved
:w

" Check if symbol is renameable
let hover = CocAction('getHover')
echo hover

" Restart language server
:CocRestart

" Check workspace folders are configured
:CocList folders

Quick Fix Not Applied

Problem: Quick fix command doesn't change the code.

Solutions:

" Check available quick fixes
let fixes = CocAction('quickfixes', 'cursor')
echo 'Available fixes: ' . len(fixes)

" Try showing all code actions
call CocAction('codeAction', 'cursor')

" Manually apply with selection
nmap <leader>ca <Plug>(coc-codeaction-cursor)

Troubleshooting

Code actions are slow to appear

Problem: Long delay when requesting code actions.

Solutions:

  1. Check language server performance: :CocCommand workspace.showOutput
  2. Reduce scope: Use cursor mode instead of buffer-wide actions
  3. Disable unused code action providers in language server config
  4. Increase timeout: Set larger timeout in LSP config

Organize imports doesn't work

Problem: Organize imports command has no effect.

Solutions:

  1. Verify language server supports organize imports:
    if CocHasProvider('organizeImport')
      echo "Supported"
    endif
  2. Check file type is correct: :set filetype?
  3. Save file first: :w
  4. Try manual import management through code actions

Rename affects wrong symbols

Problem: Rename operation changes unintended occurrences.

Solutions:

  1. Ensure cursor is on the correct symbol
  2. Use workspace-wide search first: :CocSearch <symbol>
  3. Preview changes with refactor window: <Plug>(coc-refactor)
  4. Update language server for better rename support

Code lens not showing

Problem: Code lens actions don't appear above code.

Solutions:

  1. Enable code lens: "codeLens.enable": true
  2. Check virtual text is supported: :echo has('nvim-0.5')
  3. Verify language server provides code lens:
    :CocCommand workspace.showOutput
  4. Refresh manually: :CocCommand document.refresh

See Also

  • Diagnostics - Error detection for code actions
  • Completion - Code completion features
  • Formatting - Code formatting operations
  • Navigation - Jump to definitions and references
  • Workspace - Workspace-wide operations