-
Notifications
You must be signed in to change notification settings - Fork 11
Add support for custom match functions #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| *matchit.txt* Extended "%" matching Last change: 2025 Aug 07 | ||
| *matchit.txt* Extended "%" matching Last change: 2025 Dec 23 | ||
|
|
||
| VIM REFERENCE MANUAL by Benji Fisher et al | ||
|
|
||
|
|
@@ -253,6 +253,45 @@ Examples: | |
| See the $VIMRUNTIME/ftplugin/vim.vim for an example that uses both | ||
| syntax and a regular expression. | ||
|
|
||
| *b:match_function* | ||
| If b:match_function is defined, matchit.vim will first call this function to | ||
| perform matching. This is useful for languages with an indentation-based block | ||
| structure (such as Python) or other complex matching requirements that cannot | ||
| be expressed with regular expression patterns. | ||
|
|
||
| The function should accept one argument: | ||
| forward - 1 for forward search (% command) | ||
| 0 for backward search (g% command) | ||
|
|
||
| The function should return a list with one of these values: | ||
| [line, col] - Match found at the specified position | ||
| [] - No match found; fall through to regular matching | ||
| (|b:match_words|, matchpairs, etc.) | ||
|
|
||
| The cursor position is not changed by the function; matchit handles cursor | ||
| movement based on the returned position. | ||
|
|
||
| If the function throws an error, matchit gives up and doesn't continue. | ||
| Enable |b:match_debug| to see error messages from custom match functions. | ||
|
|
||
| Python example (simplified): > | ||
| let s:keywords = {'if': 'elif\|else', 'elif': 'elif\|else'} | ||
|
|
||
| function! s:PythonMatch(forward) abort | ||
| let keyword = matchstr(getline('.'), '^\s*\zs\w\+') | ||
| let pattern = get(s:keywords, keyword, '') | ||
| if empty(pattern) | return [] | endif | ||
|
|
||
| let flags = a:forward ? 'nW' : 'nbW' | ||
| let [lnum, col] = searchpos('^\s*\%(' . pattern . '\)\>', flags, 0, 0, | ||
| \ 'indent(".") != ' . indent('.')) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This skip pattern needs to test the indent at the jump point with that at the destination.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scrap that, sorry, I misread it . |
||
| return lnum > 0 ? [lnum, col] : [] | ||
| endfunction | ||
|
|
||
| let b:match_function = function('s:PythonMatch') | ||
| < | ||
| See |matchit-newlang| below for more details on supporting new languages. | ||
|
|
||
| ============================================================================== | ||
| 4. Supporting a New Language *matchit-newlang* | ||
| *b:match_words* | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if this is somehow duplicated but I don't think it was sent with my previous comment as I expected.
This example doesn't work for
g%. It needs separate patterns when working in that direction.Perhaps it could be simplified to just work i the forward direction with a note, "backwards jumping is left as an exercise for the reader". Otherwise it should probably be improved so it can at least be dropped in as a working example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that's a good idea. I've done that in #55.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need #55 in Vim?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good documentation improvement, but it doesn't change any functionality. Do you agree @dkearns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think it would be a good idea to include it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alright, will merge it then in both projects