Skip to content

Commit e2a9dab

Browse files
authored
Merge pull request #1 from mark2185/feature/non-standard-extensions
Support for non standard extensions
2 parents 5a27af7 + e814a09 commit e2a9dab

File tree

5 files changed

+100
-36
lines changed

5 files changed

+100
-36
lines changed

README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414
Just call `:Alternate` and the window will open
1515
a matching source/header file to the currently opened one.
1616

17-
## Commands
18-
Alternate
19-
* looks for a file with the same name, but a different extension
20-
* it cycles through all the extensions, starting from the current buffer's extension
21-
22-
## Mappings
23-
The only mapping this plugin provides is:
24-
`<Plug>(Alternate)`
25-
2617
## Configuration
2718
The file extensions the plugin looks for are stored in the
2819
following arrays:
@@ -45,6 +36,31 @@ Default value is:
4536
[ 'node_modules', '.git' ]
4637
```
4738

39+
## Commands
40+
Alternate
41+
* strips the longest extension, and then looks for a file with the same name, but a different extension
42+
* it cycles through all the extensions, starting from the longest matching one
43+
44+
Note: it supports even non-standard extensions, doesn't have to be an e.g. `.hpp` (see example below)
45+
46+
```
47+
let g:alternator_source_extensions = [ '.c' ]
48+
let g:alternator_header_extensions = [ '_impl.h', '.h' ]
49+
:edit action.c
50+
:Alternate
51+
# the longest matching extension is '.c', basename is 'action',
52+
# so it will start the search with the next one, 'action_impl.h'
53+
54+
:edit action_impl.h
55+
:Alternate
56+
# the longest matching extension is '_impl.h', basename is 'action',
57+
# so it will start the search with the next one, 'action.h'
58+
```
59+
60+
## Mappings
61+
The only mapping this plugin provides is:
62+
`<Plug>(Alternate)`
63+
4864
## Bonus
4965

5066
If you have `fd` in your path, it will be used instead of vim's `findfile`.

autoload/alternator.vim

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ endfunction
1616

1717
function! s:findFiles( filename ) abort
1818
if executable( 'fd' ) == 1
19-
return systemlist( printf( 'fd --glob %s .', a:filename ) )
19+
return systemlist( printf( 'fd --color=never --glob %s .', a:filename ) )
2020
else
2121
return findfile( a:filename, '**', -1 )
2222
endif
@@ -30,9 +30,23 @@ function! alternator#alternate() abort
3030

3131
let l:all_extensions = g:alternator_header_extensions + g:alternator_source_extensions
3232

33-
let l:filename = expand( '%:t:r' )
34-
let l:extension = expand( '%:e' )
35-
let l:idx = index( l:all_extensions, '.' . extension )
33+
const buffer_name = expand( '%:t' )
34+
let l:extension = ''
35+
let l:filename = ''
36+
let l:len_max = -1
37+
for ext in l:all_extensions
38+
" echom 'Checking for... ' .. ext
39+
let l:filename = substitute( l:buffer_name, ext .. '$', '', '' )
40+
let l:length = len( l:buffer_name ) - len( l:filename )
41+
if l:length > l:len_max
42+
" echom 'This is better!'
43+
let l:len_max = l:length
44+
let l:extension = l:ext
45+
endif
46+
endfor
47+
let l:filename = substitute( l:buffer_name, l:extension .. '$', '', '' )
48+
49+
let l:idx = index( l:all_extensions, l:extension )
3650

3751
if l:idx < 0
3852
echom printf('Extension %s not supported', l:extension)

autoload/alternator9.vim

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ enddef
1717

1818
def FindFiles( filename: string ): list< string >
1919
if executable( 'fd' ) == 1
20-
return systemlist( printf( 'fd --glob %s .', filename ) )
20+
return systemlist( printf( 'fd --color=never --glob %s .', filename ) )
2121
else
2222
return findfile( filename, '**', -1 )
2323
endif
@@ -31,11 +31,29 @@ export def Alternate(): void
3131

3232
const all_extensions: list< string > = g:alternator_header_extensions + g:alternator_source_extensions
3333

34-
const filename = expand( '%:t:r' )
35-
const extension = expand( '%:e' )
36-
var idx = index( all_extensions, '.' .. extension )
34+
const buffer_name = expand( '%:t' )
35+
var extension = ''
36+
var filename = ''
37+
var len_max = -1
38+
for ext in all_extensions
39+
# echom 'Checking for... ' .. ext
40+
filename = substitute( buffer_name, ext .. '$', '', '' )
41+
const length = len( buffer_name ) - len( filename )
42+
if length > len_max
43+
# echom 'This is better!'
44+
len_max = length
45+
extension = ext
46+
endif
47+
endfor
48+
filename = substitute( buffer_name, extension .. '$', '', '' )
49+
50+
var idx = index( all_extensions, extension )
3751

3852
if idx < 0
53+
if index( [ 'c', 'cpp' ], &ft ) == -1
54+
echom printf( 'Filetype "%s" not supported! Only c and cpp are supported!', &ft )
55+
return
56+
endif
3957
echom printf( 'Extension %s not supported', extension )
4058
return
4159
endif

doc/alternator.txt

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Contents ~
55

66
1. Introduction |alternator-introduction|
77
2. Usage |alternator-usage|
8-
3. Commands |alternator-commands|
9-
4. Mappings |alternator-mappings|
10-
5. Configuration |alternator-configuration|
8+
3. Configuration |alternator-configuration|
9+
4. Commands |alternator-commands|
10+
5. Mappings |alternator-mappings|
1111
6. Bonus |alternator-bonus|
1212

1313
===============================================================================
@@ -19,8 +19,8 @@ Introduction ~
1919
//_\\| | __/ _ \ '__| '_ \ / _` | __/ _ \| '__|~
2020
/ _ \ | || __/ | | | | | (_| | || (_) | | ~
2121
\_/ \_/_|\__\___|_| |_| |_|\__,_|\__\___/|_| ~
22-
23-
Alternating between headers, source files,
22+
23+
Alternating between headers, source files,
2424
template implementations and others with blazing speed!
2525
<
2626
===============================================================================
@@ -30,19 +30,6 @@ Usage ~
3030
Just call ':Alternate' and the window will open a matching source/header file
3131
to the currently opened one.
3232

33-
===============================================================================
34-
*alternator-commands*
35-
Commands ~
36-
37-
Alternate _looks for a file with the same name, but a different extension_ it
38-
cycles through all the extensions, starting from the current buffer's extension
39-
40-
===============================================================================
41-
*alternator-mappings*
42-
Mappings ~
43-
44-
The only mapping this plugin provides is: '<Plug>(Alternate)'
45-
4633
===============================================================================
4734
*alternator-configuration*
4835
Configuration ~
@@ -64,6 +51,35 @@ Default value is:
6451
>
6552
[ 'node_modules', '.git' ]
6653
<
54+
===============================================================================
55+
*alternator-commands*
56+
Commands ~
57+
58+
Alternate _strips the longest extension, and then looks for a file with the
59+
same name, but a different extension_ it cycles through all the extensions,
60+
starting from the longest matching one
61+
62+
Note: it supports even non-standard extensions, doesn't have to be an e.g.
63+
'.hpp' (see example below)
64+
>
65+
let g:alternator_source_extensions = [ '.c' ]
66+
let g:alternator_header_extensions = [ '_impl.h', '.h' ]
67+
:edit action.c
68+
:Alternate
69+
# the longest matching extension is '.c', basename is 'action',
70+
# so it will start the search with the next one, 'action_impl.h'
71+
72+
:edit action_impl.h
73+
:Alternate
74+
# the longest matching extension is '_impl.h', basename is 'action',
75+
# so it will start the search with the next one, 'action.h'
76+
<
77+
===============================================================================
78+
*alternator-mappings*
79+
Mappings ~
80+
81+
The only mapping this plugin provides is: '<Plug>(Alternate)'
82+
6783
===============================================================================
6884
*alternator-bonus*
6985
Bonus ~

plugin/alternator.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
if exists('g:loaded_alternator_plugin')
55
finish
66
endif
7-
let g:loaded_alternator_plugin = 1
7+
const g:loaded_alternator_plugin = 1
88

99
let g:alternator_header_extensions = get( g:, 'alternator_header_extensions', [ '.h', '.hpp', '.tpp', '.ipp' ] )
1010
let g:alternator_source_extensions = get( g:, 'alternator_source_extensions', [ '.c', '.cpp', ] )

0 commit comments

Comments
 (0)