mirror of
https://github.com/neovim/neovim
synced 2025-07-15 16:51:49 +00:00
Problem: Diff mode's inline highlighting is lackluster. It only
performs a line-by-line comparison, and calculates a single
shortest range within a line that could encompass all the
changes. In lines with multiple changes, or those that span
multiple lines, this approach tends to end up highlighting
much more than necessary.
Solution: Implement new inline highlighting modes by doing per-character
or per-word diff within the diff block, and highlight only the
relevant parts, add "inline:simple" to the defaults (which is
the old behaviour)
This change introduces a new diffopt option "inline:<type>". Setting to
"none" will disable all inline highlighting, "simple" (the default) will
use the old behavior, "char" / "word" will perform a character/word-wise
diff of the texts within each diff block and only highlight the
differences.
The new char/word inline diff only use the internal xdiff, and will
respect diff options such as algorithm choice, icase, and misc iwhite
options. indent-heuristics is always on to perform better sliding.
For character highlight, a post-process of the diff results is first
applied before we show the highlight. This is because a naive diff will
create a result with a lot of small diff chunks and gaps, due to the
repetitive nature of individual characters. The post-process is a
heuristic-based refinement that attempts to merge adjacent diff blocks
if they are separated by a short gap (1-3 characters), and can be
further tuned in the future for better results. This process results in
more characters than necessary being highlighted but overall less visual
noise.
For word highlight, always use first buffer's iskeyword definition.
Otherwise if each buffer has different iskeyword settings we would not
be able to group words properly.
The char/word diffing is always per-diff block, not per line, meaning
that changes that span multiple lines will show up correctly.
Added/removed newlines are not shown by default, but if the user has
'list' set (with "eol" listchar defined), the eol character will be be
highlighted correctly for the specific newline characters.
Also, add a new "DiffTextAdd" highlight group linked to "DiffText" by
default. It allows color schemes to use different colors for texts that
have been added within a line versus modified.
This doesn't interact with linematch perfectly currently. The linematch
feature splits up diff blocks into multiple smaller blocks for better
visual matching, which makes inline highlight less useful especially for
multi-line change (e.g. a line is broken into two lines). This could be
addressed in the future.
As a side change, this also removes the bounds checking introduced to
diff_read() as they were added to mask existing logic bugs that were
properly fixed in vim/vim#16768.
closes: vim/vim#16881
9943d4790e
Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
120 lines
3.0 KiB
VimL
120 lines
3.0 KiB
VimL
if exists('s:did_load')
|
|
" Align Nvim defaults to Vim.
|
|
set commentstring=/*\ %s\ */
|
|
set complete=.,w,b,u,t,i
|
|
set define=^\\s*#\\s*define
|
|
set diffopt=internal,filler,closeoff,inline:simple
|
|
set directory^=.
|
|
set display=
|
|
set fillchars=vert:\|,foldsep:\|,fold:-
|
|
set formatoptions=tcq
|
|
set fsync
|
|
set include=^\\s*#\\s*include
|
|
set laststatus=1
|
|
set listchars=eol:$
|
|
set joinspaces
|
|
set jumpoptions=
|
|
set mousemodel=extend
|
|
set nohidden nosmarttab noautoindent noautoread noruler noshowcmd
|
|
set nohlsearch noincsearch
|
|
set nrformats=bin,octal,hex
|
|
set shortmess=filnxtToOS
|
|
set sidescroll=0
|
|
set tags=./tags,tags
|
|
set undodir^=.
|
|
set wildoptions=
|
|
set startofline
|
|
set sessionoptions+=options
|
|
set viewoptions+=options
|
|
set switchbuf=
|
|
if has('win32')
|
|
set isfname+=:
|
|
endif
|
|
if g:testname !~ 'test_mapping.vim$'
|
|
" Make "Q" switch to Ex mode.
|
|
" This does not work for all tests as Nvim only supports Vim Ex mode.
|
|
nnoremap Q gQ<Cmd>call<SID>ExStart()<CR>
|
|
endif
|
|
endif
|
|
|
|
" Common preparations for running tests.
|
|
|
|
" Only load this once.
|
|
if exists('s:did_load')
|
|
finish
|
|
endif
|
|
let s:did_load = 1
|
|
|
|
func s:ExStart()
|
|
call feedkeys($"\<Cmd>call{expand('<SID>')}ExMayEnd()\<CR>")
|
|
endfunc
|
|
|
|
func s:ExMayEnd()
|
|
" When :normal runs out of characters in Vim, the behavior is different in
|
|
" normal Ex mode vs. Vim Ex mode.
|
|
" - In normal Ex mode, "\n" is used.
|
|
" - In Vim Ex mode, Ctrl-C is used.
|
|
" Nvim only supports Vim Ex mode, so emulate the normal Ex mode behavior.
|
|
if state('m') == '' && mode(1) == 'cv' && getcharstr(1) == "\<C-C>"
|
|
call feedkeys("\n")
|
|
endif
|
|
endfunc
|
|
|
|
" Clear Nvim default user commands, mappings and menus.
|
|
comclear
|
|
mapclear
|
|
mapclear!
|
|
aunmenu *
|
|
tlunmenu *
|
|
autocmd! nvim.popupmenu
|
|
|
|
" Undo the 'grepprg' and 'grepformat' setting in _defaults.lua.
|
|
set grepprg& grepformat&
|
|
|
|
" roughly equivalent to test_setmouse() in Vim
|
|
func Ntest_setmouse(row, col)
|
|
call nvim_input_mouse('move', '', '', 0, a:row - 1, a:col - 1)
|
|
if state('m') == ''
|
|
call getchar(0)
|
|
endif
|
|
endfunc
|
|
|
|
" roughly equivalent to term_wait() in Vim
|
|
func Nterm_wait(buf, time = 10)
|
|
execute $'sleep {a:time}m'
|
|
endfunc
|
|
|
|
" Prevent Nvim log from writing to stderr.
|
|
let $NVIM_LOG_FILE = exists($NVIM_LOG_FILE) ? $NVIM_LOG_FILE : 'Xnvim.log'
|
|
|
|
" Make sure 'runtimepath' and 'packpath' does not include $HOME.
|
|
set rtp=$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after
|
|
let &packpath = &rtp
|
|
|
|
" Avoid storing shell history.
|
|
let $HISTFILE = ""
|
|
|
|
" Use default shell on Windows to avoid segfault, caused by TUI
|
|
if has('win32')
|
|
let $SHELL = ''
|
|
let $TERM = ''
|
|
let &shell = empty($COMSPEC) ? exepath('cmd.exe') : $COMSPEC
|
|
set shellcmdflag=/s/c shellxquote=\" shellredir=>%s\ 2>&1
|
|
let &shellpipe = &shellredir
|
|
endif
|
|
|
|
" Detect user modules for language providers
|
|
let $PYTHONUSERBASE = $HOME . '/.local'
|
|
if executable('gem')
|
|
let $GEM_PATH = system('gem env gempath')
|
|
endif
|
|
|
|
" Make sure $HOME does not get read or written.
|
|
let $HOME = expand(getcwd() . '/XfakeHOME')
|
|
if !isdirectory($HOME)
|
|
call mkdir($HOME)
|
|
endif
|
|
|
|
" Use Vim's default color scheme
|
|
colorscheme vim
|