Skip to content

Commit 5df91ec

Browse files
cxw42xuhdev
authored andcommitted
Add back in the option of using an external core (#128)
* Add external-command option (#121) Plugin and docs: - g:EditorConfig_exec_path holds the path to an EditorConfig core - g:EditorConfig_core_mode accepts 'external_command' to use that core - If the given executable does not exist, the plugin will automatically fall back to the 'vim_core' mode Tests: - Refactored tests slightly to be able to test more than one Vim condition - Added tests of external_command mode, which are run if the path to the core is given in environment variable $EDITORCONFIG_VIM_EXTERNAL_CORE * Fix whitespace [minor] * Updates per comments on #128
1 parent f8954f1 commit 5df91ec

5 files changed

Lines changed: 282 additions & 129 deletions

File tree

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ end_of_line = lf
55
charset = utf-8
66
max_line_length = 80
77

8-
[*.vim]
8+
[*.{vim,sh}]
99
indent_style = space
1010
indent_size = 4
1111
insert_final_newline = true

doc/editorconfig.txt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
*editorconfig.txt*
22

33
File: editorconfig.txt
4-
Version: 1.0.0-beta
4+
Version: 1.1.0-beta
55
Maintainer: EditorConfig Team <http://editorconfig.org>
66
Description: EditorConfig vim plugin
77

@@ -69,13 +69,21 @@ SETTINGS~
6969
----------------------------------------------------------------------------
7070
*g:EditorConfig_core_mode*
7171
Specify the mode of EditorConfig core. Generally it is OK to leave this option
72-
empty. Currently, the only supported mode is "vim_core".
72+
empty. Currently, the supported modes are "vim_core" (default) and
73+
"external_command".
7374

7475
vim_core: Use the included VimScript EditorConfig Core.
76+
external_command: Run external EditorConfig Core.
7577

7678
If "g:EditorConfig_core_mode" is not specified, this plugin will automatically
7779
choose "vim_core".
7880

81+
If you choose "external_command" mode, you must also set
82+
|g:EditorConfig_exec_path|.
83+
84+
Changes to "g:EditorConfig_core_mode" will not take effect until Vim
85+
is restarted.
86+
7987
*g:EditorConfig_exclude_patterns*
8088
This is a list contains file path patterns which will be ignored by
8189
EditorConfig plugin. When the path of the opened buffer (i.e.
@@ -86,6 +94,21 @@ Example: Avoid loading EditorConfig for any remote files over ssh
8694
>
8795
let g:EditorConfig_exclude_patterns = ['scp://.*']
8896
<
97+
98+
*g:EditorConfig_exec_path*
99+
The file path to the EditorConfig core executable. You can set this value in
100+
your |vimrc| like this:
101+
>
102+
let g:EditorConfig_exec_path = 'Path to your EditorConfig Core executable'
103+
<
104+
The default value is empty.
105+
106+
If "g:EditorConfig_exec_path" is not set, the plugin will use the "vim_core"
107+
mode regardless of the setting of |g:EditorConfig_core_mode|.
108+
109+
Changes to "g:EditorConfig_exec_path" will not take effect until Vim
110+
is restarted.
111+
89112
*g:EditorConfig_max_line_indicator*
90113
The way to show the line where the maximal length is reached. Accepted values
91114
are "line", "fill", otherwise there will be no max line indicator.
@@ -109,23 +132,20 @@ are "line", "fill", otherwise there will be no max line indicator.
109132
as "none".
110133

111134
To set this option, add any of the following lines to your |vimrc| file:
112-
113135
>
114136
let g:EditorConfig_max_line_indicator = "line"
115137
let g:EditorConfig_max_line_indicator = "fill"
116138
let g:EditorConfig_max_line_indicator = "exceeding"
117139
let g:EditorConfig_max_line_indicator = "none"
118140
<
119-
120-
Default to "line".
141+
The default value is "line".
121142

122143
*g:EditorConfig_preserve_formatoptions*
123144
Set this to 1 if you don't want your formatoptions modified when
124145
max_line_length is set:
125146
>
126147
let g:EditorConfig_preserve_formatoptions = 1
127148
<
128-
129149
This option defaults to 0.
130150

131151
*g:EditorConfig_verbose*

plugin/editorconfig.vim

Lines changed: 122 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ let s:saved_cpo = &cpo
3838
set cpo&vim
3939

4040
" variables {{{1
41+
42+
" Make sure the globals all exist
4143
if !exists('g:EditorConfig_exec_path')
4244
let g:EditorConfig_exec_path = ''
4345
endif
@@ -62,12 +64,20 @@ if !exists('g:EditorConfig_disable_rules')
6264
let g:EditorConfig_disable_rules = []
6365
endif
6466

67+
" Copy some of the globals into script variables --- changes to these
68+
" globals won't affect the plugin until the plugin is reloaded.
6569
if exists('g:EditorConfig_core_mode') && !empty(g:EditorConfig_core_mode)
6670
let s:editorconfig_core_mode = g:EditorConfig_core_mode
6771
else
6872
let s:editorconfig_core_mode = ''
6973
endif
7074

75+
if exists('g:EditorConfig_exec_path') && !empty(g:EditorConfig_exec_path)
76+
let s:editorconfig_exec_path = g:EditorConfig_exec_path
77+
else
78+
let s:editorconfig_exec_path = ''
79+
endif
80+
7181
let s:initialized = 0
7282

7383
" }}}1
@@ -107,50 +117,62 @@ function! s:InitializeVimCore()
107117
endtry
108118
return 0
109119
endfunction
120+
121+
function! s:InitializeExternalCommand()
122+
" Initialize external_command mode
123+
124+
if empty(s:editorconfig_exec_path)
125+
echo 'Please specify a g:EditorConfig_exec_path'
126+
return 1
127+
endif
128+
129+
if g:EditorConfig_verbose
130+
echo 'Checking for external command ' . s:editorconfig_exec_path . ' ...'
131+
endif
132+
133+
if !executable(s:editorconfig_exec_path)
134+
echo 'File ' . s:editorconfig_exec_path . ' is not executable.'
135+
return 1
136+
endif
137+
138+
return 0
139+
endfunction
110140
" }}}1
111141

112142
function! s:Initialize() " Initialize the plugin. {{{1
113143
" Returns truthy on error, falsy on success.
114144

115-
" Do some initialization if the user has specified a core mode {{{2
116-
if exists('s:editorconfig_core_mode') && !empty(s:editorconfig_core_mode)
145+
if empty(s:editorconfig_core_mode)
146+
let s:editorconfig_core_mode = 'vim_core' " Default core choice
147+
endif
117148

118-
if s:editorconfig_core_mode ==? 'vim_core'
119-
if s:InitializeVimCore()
120-
echo 'EditorConfig: Failed to initialize vim_core mode'
121-
return 1
122-
endif
123-
else
124-
echo "EditorConfig: I don't know how to use mode " . s:editorconfig_core_mode
125-
return 1
149+
if s:editorconfig_core_mode ==? 'external_command'
150+
if s:InitializeExternalCommand()
151+
echohl WarningMsg
152+
echo 'EditorConfig: Failed to initialize external_command mode. ' .
153+
\ 'Falling back to vim_core mode.'
154+
echohl None
155+
let s:editorconfig_core_mode = 'vim_core'
126156
endif
157+
endif
127158

128-
endif " }}}2
129-
130-
" Determine the editorconfig_core_mode we should use {{{2
131-
while 1
132-
" If user has specified a mode, just break
133-
if exists('s:editorconfig_core_mode') && !empty(s:editorconfig_core_mode)
134-
break
159+
if s:editorconfig_core_mode ==? 'vim_core'
160+
if s:InitializeVimCore()
161+
echohl ErrorMsg
162+
echo 'EditorConfig: Failed to initialize vim_core mode. ' .
163+
\ 'The plugin will not function.'
164+
echohl None
165+
return 1
135166
endif
136167

137-
" Try the Vimscript core
138-
try
139-
let l:vim_core_ver = editorconfig_core#version()
140-
let s:editorconfig_core_mode = 'vim_core'
141-
break
142-
catch
143-
" if the Vim core wasn't loaded, we will report it below
144-
endtry
145-
146-
break
147-
endwhile " }}}2
168+
elseif s:editorconfig_core_mode ==? 'external_command'
169+
" Nothing to do here, but this elseif is required to avoid
170+
" external_command falling into the else clause.
148171

149-
" No EditorConfig Core is available
150-
if empty(s:editorconfig_core_mode)
151-
echo "EditorConfig: ".
152-
\ "No EditorConfig Core is available. " .
153-
\ "The plugin won't work."
172+
else " neither external_command nor vim_core
173+
echohl ErrorMsg
174+
echo "EditorConfig: I don't know how to use mode " . s:editorconfig_core_mode
175+
echohl None
154176
return 1
155177
endif
156178

@@ -214,6 +236,8 @@ function! s:UseConfigFiles() abort " Apply config to the current buffer {{{1
214236

215237
if s:editorconfig_core_mode ==? 'vim_core'
216238
call s:UseConfigFiles_VimCore()
239+
elseif s:editorconfig_core_mode ==? 'external_command'
240+
call s:UseConfigFiles_ExternalCommand()
217241
else
218242
echohl Error |
219243
\ echo "Unknown EditorConfig Core: " .
@@ -268,6 +292,71 @@ function! s:UseConfigFiles_VimCore()
268292
endtry
269293
endfunction
270294

295+
function! s:UseConfigFiles_ExternalCommand()
296+
" Use external EditorConfig core (e.g., the C core)
297+
298+
call s:DisableShellSlash()
299+
let l:exec_path = shellescape(s:editorconfig_exec_path)
300+
call s:ResetShellSlash()
301+
302+
call s:SpawnExternalParser(l:exec_path)
303+
endfunction
304+
305+
function! s:SpawnExternalParser(cmd) " {{{2
306+
" Spawn external EditorConfig. Used by s:UseConfigFiles_ExternalCommand()
307+
308+
let l:cmd = a:cmd
309+
310+
if empty(l:cmd)
311+
throw 'No cmd provided'
312+
endif
313+
314+
let l:config = {}
315+
316+
call s:DisableShellSlash()
317+
let l:cmd = l:cmd . ' ' . shellescape(expand('%:p'))
318+
call s:ResetShellSlash()
319+
320+
let l:parsing_result = split(system(l:cmd), '\v[\r\n]+')
321+
322+
" if editorconfig core's exit code is not zero, give out an error
323+
" message
324+
if v:shell_error != 0
325+
echohl ErrorMsg
326+
echo 'Failed to execute "' . l:cmd . '". Exit code: ' .
327+
\ v:shell_error
328+
echo ''
329+
echo 'Message:'
330+
echo l:parsing_result
331+
echohl None
332+
return
333+
endif
334+
335+
if g:EditorConfig_verbose
336+
echo 'Output from EditorConfig core executable:'
337+
echo l:parsing_result
338+
endif
339+
340+
for one_line in l:parsing_result
341+
let l:eq_pos = stridx(one_line, '=')
342+
343+
if l:eq_pos == -1 " = is not found. Skip this line
344+
continue
345+
endif
346+
347+
let l:eq_left = strpart(one_line, 0, l:eq_pos)
348+
if l:eq_pos + 1 < strlen(one_line)
349+
let l:eq_right = strpart(one_line, l:eq_pos + 1)
350+
else
351+
let l:eq_right = ''
352+
endif
353+
354+
let l:config[l:eq_left] = l:eq_right
355+
endfor
356+
357+
call s:ApplyConfig(l:config)
358+
endfunction " }}}2
359+
271360
" }}}1
272361

273362
function! s:ApplyConfig(config) abort " Set the buffer options {{{1

0 commit comments

Comments
 (0)