Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

On the fly highlighting of search string #28

Closed
wants to merge 1 commit into from

3 participants

@mong8se
  • does nothing if vim wasn't compiled with +conceal
  • underlines first instance of each character as you type

This is sort of how quicksilver works, so for instance:

if you type cmaw you would see this (fake underlining):

ruby/command-t/match_window.rb
     - - -           -

It'd be easy to change the highlighting to be a color instead, or even to make it configurable by the user.

While I was in there, I also changed some class variables to constants, as their values never changed.

Steven Moazami Added highlighting of entered characters in filenames
* does nothing if vim wasn't compiled with +conceal
* underlines first instance of each character as you type
a85ad1b
@benmills

I tried this out and it seems to work great, I would love to see this pulled in. +1

@wincent
Owner

Thanks for submitting the pull request. It's a really nice feature.

I've merged it into a branch with some minor tweaks (it needed rebasing, some whitespace fixes etc). It seems to work well, but as I am not sure of the performance impact (I suspect it's minor, but...) I just want to kick the tires on it for a while before I merge the branch into master.

https://github.com/wincent/Command-T/tree/mong8se/highlight

(Might take a few minutes to mirror)

@wincent wincent closed this
@blueyed blueyed referenced this pull request from a commit in blueyed/Command-T
Steven Moazami Added highlighting of entered characters in filenames
  - does nothing if vim wasn't compiled with +conceal
  - underlines first instance of each character as you type
  - replaced class variables which never get modified with constants

Amendments made to original pull request[1]:

  - whitespace fixes (removed blank lines, replaced 4-space indents with
    2-space ones)
  - wrapped some long lines
  - added comments to the `#match_with_syntax_highlight` method noting
    its limitations
  - reimplemented the guts of that method in terms of a single iteration
    rather than nested interations (the nested iterations could lead to
    highlighting being repeatedly applied to the same character; for
    example, consider the search abbrevation "aba" and the matched path
    "aba", which would highlight the first "a" twice)

[1]: wincent#28

Signed-off-by: Wincent Colaiuta <win@wincent.com>
d3b0b1a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 22, 2011
  1. Added highlighting of entered characters in filenames

    Steven Moazami authored
    * does nothing if vim wasn't compiled with +conceal
    * underlines first instance of each character as you type
This page is out of date. Refresh to see the latest.
Showing with 48 additions and 17 deletions.
  1. +44 −17 ruby/command-t/match_window.rb
  2. +4 −0 ruby/command-t/vim.rb
View
61 ruby/command-t/match_window.rb
@@ -26,10 +26,13 @@
module CommandT
class MatchWindow
- @@selection_marker = '> '
- @@marker_length = @@selection_marker.length
- @@unselected_marker = ' ' * @@marker_length
- @@buffer = nil
+ SELECTION_MARKER = '> '
+ MARKER_LENGTH = SELECTION_MARKER.length
+ UNSELECTED_MARKER = ' ' * MARKER_LENGTH
+ MH_START = '<commandt>'
+ MH_END = '</commandt>'
+ @@buffer = nil
+
def initialize options = {}
@prompt = options[:prompt]
@@ -87,9 +90,17 @@ def initialize options = {}
# syntax coloring
if VIM::has_syntax?
- ::VIM::command "syntax match CommandTSelection \"^#{@@selection_marker}.\\+$\""
+ ::VIM::command "syntax match CommandTSelection \"^#{SELECTION_MARKER}.\\+$\""
::VIM::command 'syntax match CommandTNoEntries "^-- NO MATCHES --$"'
::VIM::command 'syntax match CommandTNoEntries "^-- NO SUCH FILE OR DIRECTORY --$"'
+
+ if VIM::has_conceal?
+ ::VIM::command 'setlocal conceallevel=2'
+ ::VIM::command 'setlocal concealcursor=nvic'
+ ::VIM::command "syntax region CommandTCharMatched matchgroup=CommandTCharMatched start=+#{MH_START}+ matchgroup=CommandTCharMatchedEnd end=+#{MH_END}+ concealends"
+ ::VIM::command 'highlight def CommandTCharMatched term=bold,underline cterm=bold,underline gui=bold,underline'
+ end
+
::VIM::command 'highlight link CommandTSelection Visual'
::VIM::command 'highlight link CommandTNoEntries Error'
::VIM::evaluate 'clearmatches()'
@@ -273,15 +284,31 @@ def restore_window_dimensions
end
def match_text_for_idx idx
- match = truncated_match @matches[idx]
- if idx == @selection
- prefix = @@selection_marker
- suffix = padding_for_selected_match match
- else
- prefix = @@unselected_marker
- suffix = ''
- end
- prefix + match + suffix
+ match = truncated_match @matches[idx]
+ if idx == @selection
+ prefix = SELECTION_MARKER
+ suffix = padding_for_selected_match match
+ else
+ if VIM::has_syntax? && VIM::has_conceal?
+ match = add_syntax_highlight match
+ end
+ prefix = UNSELECTED_MARKER
+ suffix = ''
+ end
+ prefix + match + suffix
+ end
+
+ def add_syntax_highlight match
+ list = match.split(//)
+ @prompt.abbrev.split(//).each do |ch|
+ list.each_index do |i|
+ if list[i] == ch
+ list[i] = [MH_START, ch, MH_END]
+ break
+ end
+ end
+ end
+ list.flatten.join
end
# Print just the specified match.
@@ -323,10 +350,10 @@ def print_matches
# highlighting extends all the way to the right edge of the window.
def padding_for_selected_match str
len = str.length
- if len >= @window_width - @@marker_length
+ if len >= @window_width - MARKER_LENGTH
''
else
- ' ' * (@window_width - @@marker_length - len)
+ ' ' * (@window_width - MARKER_LENGTH - len)
end
end
@@ -334,7 +361,7 @@ def padding_for_selected_match str
# window width.
def truncated_match str
len = str.length
- available_width = @window_width - @@marker_length
+ available_width = @window_width - MARKER_LENGTH
return str if len <= available_width
left = (available_width / 2) - 1
right = (available_width / 2) - 2 + (available_width % 2)
View
4 ruby/command-t/vim.rb
@@ -30,6 +30,10 @@ def self.has_syntax?
::VIM::evaluate('has("syntax")').to_i != 0
end
+ def self.has_conceal?
+ ::VIM::evaluate('has("conceal")').to_i != 0
+ end
+
def self.pwd
::VIM::evaluate 'getcwd()'
end
Something went wrong with that request. Please try again.