diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-10-12 17:33:32 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-10-12 17:33:32 +0800 |
commit | 12efa3035f85df148b23e54e051b0769572d1589 (patch) | |
tree | 08f16d7317068e6410086dcf88c16041413e9ce1 /src | |
parent | 87dd78bf46c4ff0d02f851acd927e3d3f521392c (diff) | |
download | lua-language-server-12efa3035f85df148b23e54e051b0769572d1589.zip |
先打印一下标准输入
Diffstat (limited to 'src')
-rw-r--r-- | src/ffi/sleep.lua | 8 | ||||
-rw-r--r-- | src/ffi/unicode.lua | 49 | ||||
-rw-r--r-- | src/global_protect.lua | 10 | ||||
-rw-r--r-- | src/service/init.lua | 36 |
4 files changed, 100 insertions, 3 deletions
diff --git a/src/ffi/sleep.lua b/src/ffi/sleep.lua new file mode 100644 index 00000000..5c4be639 --- /dev/null +++ b/src/ffi/sleep.lua @@ -0,0 +1,8 @@ +local ffi = require 'ffi' +ffi.cdef[[ + void Sleep(unsigned long dwMilliseconds); +]] + +return function (time) + ffi.C.Sleep(time) +end diff --git a/src/ffi/unicode.lua b/src/ffi/unicode.lua new file mode 100644 index 00000000..734b4679 --- /dev/null +++ b/src/ffi/unicode.lua @@ -0,0 +1,49 @@ +local ffi = require 'ffi' +ffi.cdef[[ + int MultiByteToWideChar(unsigned int CodePage, unsigned long dwFlags, const char* lpMultiByteStr, int cbMultiByte, wchar_t* lpWideCharStr, int cchWideChar); + int WideCharToMultiByte(unsigned int CodePage, unsigned long dwFlags, const wchar_t* lpWideCharStr, int cchWideChar, char* lpMultiByteStr, int cchMultiByte, const char* lpDefaultChar, int* pfUsedDefaultChar); +]] + +local CP_UTF8 = 65001 +local CP_ACP = 0 + +local function u2w(input) + local wlen = ffi.C.MultiByteToWideChar(CP_UTF8, 0, input, #input, nil, 0) + local wstr = ffi.new('wchar_t[?]', wlen+1) + ffi.C.MultiByteToWideChar(CP_UTF8, 0, input, #input, wstr, wlen) + return wstr, wlen +end + +local function a2w(input) + local wlen = ffi.C.MultiByteToWideChar(CP_ACP, 0, input, #input, nil, 0) + local wstr = ffi.new('wchar_t[?]', wlen+1) + ffi.C.MultiByteToWideChar(CP_ACP, 0, input, #input, wstr, wlen) + return wstr, wlen +end + +local function w2u(wstr, wlen) + local len = ffi.C.WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, nil, 0, nil, nil) + local str = ffi.new('char[?]', len+1) + ffi.C.WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, str, len, nil, nil) + return ffi.string(str) +end + +local function w2a(wstr, wlen) + local len = ffi.C.WideCharToMultiByte(CP_ACP, 0, wstr, wlen, nil, 0, nil, nil) + local str = ffi.new('char[?]', len) + ffi.C.WideCharToMultiByte(CP_ACP, 0, wstr, wlen, str, len, nil, nil) + return ffi.string(str) +end + +return { + u2w = u2w, + a2w = a2w, + w2u = w2u, + w2a = w2a, + u2a = function (input) + return w2a(u2w(input)) + end, + a2u = function (input) + return w2u(a2w(input)) + end, +} diff --git a/src/global_protect.lua b/src/global_protect.lua new file mode 100644 index 00000000..6c736ea6 --- /dev/null +++ b/src/global_protect.lua @@ -0,0 +1,10 @@ +local mt = {} +setmetatable(_G, mt) + +function mt:__index(k) + error(('读取不存在的全局变量[%s]'):format(k), 2) +end + +function mt:__newindex(k, v) + error(('保存全局变量[%s] = [%s]'):format(k, v), 2) +end diff --git a/src/service/init.lua b/src/service/init.lua index 91e0a4d4..53e6e4b3 100644 --- a/src/service/init.lua +++ b/src/service/init.lua @@ -1,5 +1,35 @@ -local api = { - definition = require 'service.definition' +local sleep = require 'ffi.sleep' +local ext = require 'process.ext' + +local function listen(self, input, output) + if input then + log.info('指定输入文件,路径为:', input) + fs.create_directories(input:parent_path()) + io.input(io.open(input:string(), 'rb')) + else + ext.set_filemode(io.stdin, 'b') + end + if output then + log.info('指定输出文件,路径为:', output) + fs.create_directories(output:parent_path()) + io.output(io.open(output:string(), 'wb')) + else + ext.set_filemode(io.stdout, 'b') + io.stdout:setvbuf 'no' + end + + for line in io.input():lines 'L' do + log.debug('标准输入:\n', line) + end +end + +local mt = { + definition = require 'service.definition', + listen = listen, } +mt.__index = mt -return api +return function () + local session = setmetatable({}, mt) + return session +end |