commit d8b9208426f52492254db2f0b8c8427e421f990c
parent 9e6a397cbf62135cf01080b1cd6b4fd7285a7319
Author: Tomas Nemec <nemi@skaut.cz>
Date: Sun, 24 Apr 2022 00:11:46 +0200
update
Diffstat:
3 files changed, 104 insertions(+), 58 deletions(-)
diff --git a/ftplugin/dart.lua b/ftplugin/dart.lua
@@ -1,20 +1,27 @@
vim.g.dart_style_guide = 2
vim.g.dart_html_in_string = true
-local opts = { buffer = nil }
+local cmd_opts = { buffer = nil }
-- vim.cmd [[command! -buffer DartAnalyzer lua require('tms.ft.dart.analyze').qf2131()]]
-vim.api.nvim_create_user_command('DartDebug', require('tms.ft.dart.debug').func, opts)
-vim.api.nvim_create_user_command('DartPrint', require('tms.ft.dart.debug').print, opts)
-vim.api.nvim_create_user_command('DartOrganizeImports', require('tms.ft.dart.lsp').organize_imports, opts)
-vim.api.nvim_create_user_command('DartFixAll', require('tms.ft.dart.lsp').fix_all, opts)
-vim.api.nvim_create_user_command('DartExtract', require('tms.ft.dart.lsp').extract_method, opts)
-vim.api.nvim_create_user_command('DartVariable', require('tms.ft.dart.lsp').extract_local_variable, opts)
+vim.api.nvim_create_user_command('DartDebug', require('tms.ft.dart.debug').func, cmd_opts)
+vim.api.nvim_create_user_command('DartPrint', require('tms.ft.dart.debug').print, cmd_opts)
+vim.api.nvim_create_user_command('DartOrganizeImports', require('tms.ft.dart.lsp').organize_imports, cmd_opts)
+vim.api.nvim_create_user_command('DartFixAll', require('tms.ft.dart.lsp').fix_all, cmd_opts)
+vim.api.nvim_create_user_command('DartExtract', require('tms.ft.dart.lsp').extract_method, cmd_opts)
+vim.api.nvim_create_user_command('DartVariable', require('tms.ft.dart.lsp').extract_local_variable, cmd_opts)
-opts = { buffer = true, noremap = true }
-vim.keymap.set('n', '<leader>pp', function() require('tms.ft.dart.debug').print() end, opts)
-vim.keymap.set('n', '<leader>pa', function() require('tms.ft.dart.analyze').qf2131() end, opts)
-vim.keymap.set('n', '<leader>po', function() require('tms.ft.dart.lsp').organize_imports() end, opts)
-vim.keymap.set('n', '<leader>pf', function() require('tms.ft.dart.lsp').fix_all() end, opts)
+local opts = function(desc) return { buffer = true, noremap = true, desc = desc } end
+vim.keymap.set('n', '<leader>pp', function() require('tms.ft.dart.debug').print() end, opts('Dart Debug Print'))
+vim.keymap.set('n', '<leader>pa', function() require('tms.ft.dart.analyze').qf2131() end, opts('Dart Analyze'))
+vim.keymap.set('n', '<leader>po', function() require('tms.ft.dart.lsp').organize_imports() end,
+ opts('Dart Organize Imports'))
+vim.keymap.set('n', '<leader>pf', function() require('tms.ft.dart.lsp').fix_all() end, opts('Dart Fix All'))
+vim.keymap.set({ 'v', 'n' }, '<leader>pi', function() require('tms.ft.dart.lsp').inline_local_variable() end,
+ opts('Dart Inline Local Varibale'))
+vim.keymap.set({ 'v', 'n' }, '<leader>pe', function() require('tms.ft.dart.lsp').extract_local_variable() end,
+ opts('Dart Extract Local Varibale'))
+vim.keymap.set({ 'v', 'n' }, '<leader>pm', function() require('tms.ft.dart.lsp').extract_method() end,
+ opts('Dart Extract Method'))
if vim.fn.getline(1):match('^#!.*dcli') then
vim.cmd [[comp dcli]]
diff --git a/lua/tms/ft/dart/lsp.lua b/lua/tms/ft/dart/lsp.lua
@@ -1,59 +1,101 @@
-local lsp = vim.lsp
-local api = vim.api
-
local M = {}
-local execute_command = function(command)
- local cur_buf = api.nvim_get_current_buf()
- local cur_buf_name = api.nvim_buf_get_name(cur_buf)
- local params = { command = command, arguments = { cur_buf_name }, title = '' }
- lsp.buf_request_sync(cur_buf, 'workspace/executeCommand', params, 1500)
+local get_visual_lines = function()
+ local vs_start_line = vim.fn.getcurpos()[2]
+ local vs_end_line = vim.fn.line('v')
+ if vs_start_line > vs_end_line then
+ return vs_end_line - 1, vs_start_line - 1
+ else
+ return vs_start_line - 1, vs_end_line - 1
+ end
end
local get_visual = function()
- local vs_start = vim.fn.getcurpos()[2]
- local vs_end = vim.fn.line('v')
- if vs_start > vs_end then
- return vs_end - 1, vs_start - 1
+ local curpos = vim.fn.getcurpos()
+ local one = { row = curpos[2] - 1, col = curpos[3] - 1 }
+ local two = { row = vim.fn.line('v') - 1, col = vim.fn.col('v') - 1 }
+
+ if one.row == two.row then
+ if one.col > two.col then
+ local tmp = one
+ one = two
+ two = tmp
+ end
+ elseif one.row > two.row then
+ local tmp = one
+ one = two
+ two = tmp
+ end
+
+ two.col = two.col + 1
+
+ return { start = one, ['end'] = two }
+end
+
+-- TODO(tms) 22.04.22: possible bug that does not count new lines
+local get_offest_length = function(buf, mode)
+ local vs = get_visual()
+ D(mode, vs)
+
+ local start_row_offset = vim.api.nvim_buf_get_offset(buf, vs.start.row)
+ local start_col_offset = vim.lsp.util.character_offset(buf, vs.start.row, vs.start.col, 'utf-8')
+ local start_offset = start_row_offset + start_col_offset
+
+ if mode == 'v' then
+ local end_row_offset = vim.api.nvim_buf_get_offset(buf, vs['end'].row)
+ local end_col_offset = vim.lsp.util.character_offset(buf, vs['end'].row, vs['end'].col, 'utf-8')
+ local end_offset = end_row_offset + end_col_offset
+ local visual_length = end_offset - start_offset
+ return start_offset, visual_length, end_offset
+ elseif mode == 'V' then
+ -- move offset to next line to count this one
+ local end_row_offset = vim.api.nvim_buf_get_offset(buf, vs['end'].row + 1)
+ local visual_length = end_row_offset - start_row_offset
+ return start_row_offset, visual_length, end_row_offset
else
- return vs_start - 1, vs_end - 1
+ return start_offset, 0
end
end
-M.organize_imports = function() execute_command('edit.organizeImports') end
-M.fix_all = function() execute_command('edit.fixAll') end
-M.extract_method = function()
- local buf = vim.api.nvim_get_current_buf()
+local make_params = function(buf, mode, command)
local buf_name = vim.api.nvim_buf_get_name(buf)
- local start, stop = get_visual()
- local start_offset = vim.api.nvim_buf_get_offset(buf, start)
- local end_offset = vim.api.nvim_buf_get_offset(buf, stop + 1)
- local length = end_offset - start_offset
- vim.ui.input({}, function(input)
- if input and #input ~= 0 then
- vim.lsp.buf_request(buf, 'workspace/executeCommand', {
- command = 'refactor.perform',
- arguments = { 'EXTRACT_METHOD', buf_name, nil, start_offset, length, { name = input } },
- })
- end
- end)
+ local offset, length = get_offest_length(buf, mode)
+ return { command, buf_name, nil, offset, length }
+end
+
+local execute_simple_command = function(command)
+ local cur_buf = vim.api.nvim_get_current_buf()
+ local cur_buf_name = vim.api.nvim_buf_get_name(cur_buf)
+ local params = { command = command, arguments = { cur_buf_name }, title = '' }
+ vim.lsp.buf_request_sync(cur_buf, 'workspace/executeCommand', params)
end
--- TODO(tms) 20.04.22:
-M.extract_local_variable = function()
+
+local execute_command = function(command)
+ local mode = vim.api.nvim_get_mode().mode
local buf = vim.api.nvim_get_current_buf()
- local buf_name = vim.api.nvim_buf_get_name(buf)
- local start, stop = get_visual()
- local start_offset = vim.api.nvim_buf_get_offset(buf, start)
- local end_offset = vim.api.nvim_buf_get_offset(buf, stop + 1)
- local length = end_offset - start_offset
+ local params = make_params(buf, mode, command)
+ D(params)
vim.ui.input({}, function(input)
if input and #input ~= 0 then
- vim.lsp.buf_request(buf, 'workspace/executeCommand', {
- command = 'refactor.perform',
- arguments = { 'EXTRACT_LOCAL_VARIABLE', buf_name, nil, start_offset, length, { name = input } },
- })
+ table.insert(params, { name = input })
+ vim.lsp.buf_request(buf, 'workspace/executeCommand', { command = 'refactor.perform', arguments = params })
end
end)
end
+local execute_command_no_input = function(command)
+ local mode = vim.api.nvim_get_mode().mode
+ local buf = vim.api.nvim_get_current_buf()
+ local params = make_params(buf, mode, command)
+ table.insert(params, { name = '' })
+ D(params)
+ vim.lsp.buf_request(buf, 'workspace/executeCommand', { command = 'refactor.perform', arguments = params })
+end
+
+M.organize_imports = function() execute_simple_command('edit.organizeImports') end
+M.fix_all = function() execute_simple_command('edit.fixAll') end
+M.extract_method = function() execute_command('EXTRACT_METHOD') end
+M.extract_local_variable = function() execute_command('EXTRACT_LOCAL_VARIABLE') end
+M.inline_local_variable = function() execute_command_no_input('INLINE_LOCAL_VARIABLE') end
+
return M
diff --git a/test/dart.dart b/test/dart.dart
@@ -1,9 +1,6 @@
-class Test {
+Class Test {
Test() {
- var test = '';
- test += 'hej';
- print(test);
+ var object = 2 + 2;
+ print(object);
}
-
- void a() => '';
}