From 64370b70d6d6bcf46be424ec78bdb85aa591d6e5 Mon Sep 17 00:00:00 2001 From: Dmitry Maluka Date: Tue, 20 Oct 2020 22:10:41 +0200 Subject: [PATCH] Highlighting tab errors Added option `hltaberrors` which helps to spot sloppy whitespace errors with tabs used instead of spaces or vice versa. It uses the value of `tabstospaces` option as a criterion whether a tab or space character is an error or not. If `tabstospaces` is on, we probably expect that the file should contain no tab characters, so any tab character is highlighted as an error. If `tabstospaces` is off, we probably expect that the file uses indentation with tabs, so space characters in the initial indent part of lines are highlighted as errors. --- internal/config/settings.go | 1 + internal/display/bufwindow.go | 14 ++++++++++++++ runtime/help/options.md | 7 +++++++ 3 files changed, 22 insertions(+) diff --git a/internal/config/settings.go b/internal/config/settings.go index 3b9bfaef..40e271b7 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -289,6 +289,7 @@ var defaultCommonSettings = map[string]interface{}{ "fileformat": defaultFileFormat(), "filetype": "unknown", "hlsearch": false, + "hltaberrors": false, "incsearch": true, "ignorecase": true, "indentchar": " ", diff --git a/internal/display/bufwindow.go b/internal/display/bufwindow.go index 6e67f845..8d4645e0 100644 --- a/internal/display/bufwindow.go +++ b/internal/display/bufwindow.go @@ -495,6 +495,8 @@ func (w *BufWindow) displayBuffer() { vloc.X = w.gutterOffset } + leadingwsEnd := len(util.GetLeadingWhitespace(b.LineBytes(bloc.Y))) + line, nColsBeforeStart, bslice, startStyle := w.getStartInfo(w.StartCol, bloc.Y) if startStyle != nil { curStyle = *startStyle @@ -518,6 +520,18 @@ func (w *BufWindow) displayBuffer() { // over cursor-line and color-column dontOverrideBackground := origBg != defBg + if b.Settings["hltaberrors"].(bool) { + if s, ok := config.Colorscheme["tab-error"]; ok { + isTab := (r == '\t') || (r == ' ' && !showcursor) + if (b.Settings["tabstospaces"].(bool) && isTab) || + (!b.Settings["tabstospaces"].(bool) && bloc.X < leadingwsEnd && r == ' ' && !isTab) { + fg, _, _ := s.Decompose() + style = style.Background(fg) + dontOverrideBackground = true + } + } + } + for _, c := range cursors { if c.HasSelection() && (bloc.GreaterEqual(c.CurSelection[0]) && bloc.LessThan(c.CurSelection[1]) || diff --git a/runtime/help/options.md b/runtime/help/options.md index a492bc44..3826f27b 100644 --- a/runtime/help/options.md +++ b/runtime/help/options.md @@ -174,6 +174,13 @@ Here are the available options: default value: `false` +* `hltaberrors`: highlight tabs when spaces are expected, and spaces when tabs + are expected. More precisely: if `tabstospaces` option is on, highlight + all tab characters; if `tabstospaces` is off, highlight space characters + in the initial indent part of the line. + + default value: `false` + * `incsearch`: enable incremental search in "Find" prompt (matching as you type). default value: `true`