Skip to content

Commit 1280c7d

Browse files
ShougoLumaKernel
andauthored
Add environment variable support (#14)
* Add environment variable support * Use Vim environment instead * Use vars instead * Add/fix for windows sep and powershell/cmd env style Co-authored-by: Luma <world@luma.email>
1 parent 0360a76 commit 1280c7d

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

autoload/ddc_file/internal.vim

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ function! s:line_to_file_full(line, is_posix) abort
22
return matchlist(
33
\ a:line,
44
\ a:is_posix
5-
\ ? '[[:fname:]/]*$'
6-
\ : '[[:fname:]\\]*$'
5+
\ ? '\c\%(^\$\%(env:\)\?\)\?[[:fname:]/]*$'
6+
\ : '\c\%(^\$\%(env:\)\?\)\?[[:fname:]\\]*$'
77
\ )[0]
88
endfunction
99

@@ -56,6 +56,9 @@ function! ddc_file#internal#_test() abort
5656
call assert_equal('../bb/aa', s:line_to_file_full('aa ff ../bb/aa', v:true))
5757
call assert_equal('a', s:line_to_file_full('\a', v:true))
5858
call assert_equal('./a', s:line_to_file_full('\./a', v:true))
59+
call assert_equal('$Foo/bar', s:line_to_file_full('$Foo/bar', v:true))
60+
call assert_equal('/bar', s:line_to_file_full('$Foo /bar', v:true))
61+
call assert_equal('Foo/bar', s:line_to_file_full('$ Foo/bar', v:true))
5962
call assert_equal('', s:full_to_base_prefix('', v:true))
6063
call assert_equal('', s:full_to_base_prefix('a', v:true))
6164
call assert_equal('', s:full_to_base_prefix('abc', v:true))
@@ -103,6 +106,14 @@ function! ddc_file#internal#_test() abort
103106
call assert_equal('..\bb\aa', s:line_to_file_full('aa ff ..\bb\aa', v:false))
104107
call assert_equal('/a', s:line_to_file_full('/a', v:false))
105108
call assert_equal('/.\a', s:line_to_file_full('/.\a', v:false))
109+
call assert_equal('$Foo/bar', s:line_to_file_full('$Foo/bar', v:false))
110+
call assert_equal('/bar', s:line_to_file_full('$Foo /bar', v:false))
111+
call assert_equal('Foo/bar', s:line_to_file_full('$ Foo/bar', v:false))
112+
call assert_equal('$env:Foo/bar', s:line_to_file_full('$env:Foo/bar', v:false))
113+
call assert_equal('/root/$env:Foo/bar', s:line_to_file_full('/root/$env:Foo/bar', v:false))
114+
call assert_equal('$Env:Foo/bar', s:line_to_file_full('$Env:Foo/bar', v:false))
115+
call assert_equal('/bar', s:line_to_file_full('$env:Foo /bar', v:false))
116+
call assert_equal('Foo/bar', s:line_to_file_full('$env: Foo/bar', v:false))
106117
call assert_equal('', s:full_to_base_prefix('', v:false))
107118
call assert_equal('', s:full_to_base_prefix('a', v:false))
108119
call assert_equal('', s:full_to_base_prefix('abc', v:false))

denops/@ddc-sources/file.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
GatherCandidatesArguments,
66
homeDir,
77
path as univPath,
8+
vars,
89
wrapA,
910
} from "../@ddc-file/deps.ts";
1011
import * as util from "../@ddc-file/util.ts";
@@ -86,7 +87,7 @@ export class Source extends BaseSource<Params> {
8687
);
8788

8889
// e.g. '/home/ubuntu/config' for inputFileFull = '~/config'
89-
const inputFileFullExpanded = (() => {
90+
const inputFileFullExpanded = await (async () => {
9091
const home = homeDir();
9192
const last = inputFileFull.endsWith(path.sep) ? path.sep : "";
9293
{
@@ -109,6 +110,22 @@ export class Source extends BaseSource<Params> {
109110
return path.join(home, inputFileFull.slice(pat.length)) + last;
110111
}
111112
}
113+
{
114+
const pat = new RegExp(
115+
`^(?:\\$(?:env:)?(\\w+)|%(\\w+)%)${path.sep === "/" ? "/" : "\\\\"}`,
116+
"i",
117+
);
118+
const m = inputFileFull.match(pat);
119+
if (m) {
120+
const env = await vars.environment.get(
121+
args.denops,
122+
m[1] || m[2],
123+
) as string;
124+
if (env) {
125+
return path.join(env, inputFileFull.slice(m[0].length)) + last;
126+
}
127+
}
128+
}
112129
return inputFileFull;
113130
})();
114131

0 commit comments

Comments
 (0)