From e80116cee03af66bb229c3f570f0b2f244f0a197 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sun, 16 Apr 2017 00:16:48 +0100 Subject: [PATCH] #427 Add a function for looking up ALE variables in buffer scope, and then global scope --- autoload/ale.vim | 11 +++++++++++ test/test_ale_var.vader | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/test_ale_var.vader diff --git a/autoload/ale.vim b/autoload/ale.vim index 3c15f5d..80ef3ee 100644 --- a/autoload/ale.vim +++ b/autoload/ale.vim @@ -96,3 +96,14 @@ endfunction function! ale#ResetLintFileMarkers() abort let s:should_lint_file_for_buffer = {} endfunction + +" Given a buffer number and a variable name, look for that variable in the +" buffer scope, then in global scope. If the name does not exist in the global +" scope, an exception will be thrown. +" +" Every variable name will be prefixed with 'ale_'. +function! ale#Var(buffer, variable_name) abort + let l:full_name = 'ale_' . a:variable_name + + return getbufvar(a:buffer, l:full_name, g:[l:full_name]) +endfunction diff --git a/test/test_ale_var.vader b/test/test_ale_var.vader new file mode 100644 index 0000000..aee8e04 --- /dev/null +++ b/test/test_ale_var.vader @@ -0,0 +1,16 @@ +Before: + let g:ale_some_variable = 'abc' + +After: + unlet! g:ale_some_variable + +Execute(ale#Var should return global variables): + AssertEqual 'abc', ale#Var(bufnr(''), 'some_variable') + +Execute(ale#Var should return buffer overrides): + let b:ale_some_variable = 'def' + + AssertEqual 'def', ale#Var(bufnr(''), 'some_variable') + +Execute(ale#Var should throw exceptions for undefined variables): + AssertThrows call ale#Var(bufnr(''), 'undefined_variable_name')