From 222721c7efdadae951defc649879c7a1c0f93beb Mon Sep 17 00:00:00 2001 From: Minh-Tam TRAN Date: Sat, 21 Aug 2021 15:40:54 +0200 Subject: [PATCH 1/6] feature (sln_scope_handling): stop running job only if completely covered by newly started job. Use g:OmniSharp_stop_redundant_servers to change that default behavior --- autoload/OmniSharp.vim | 13 +++++---- autoload/OmniSharp/actions/workspace.vim | 35 ++++++++++++++++++++++++ test/fixusings.vader | 15 ++++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/autoload/OmniSharp.vim b/autoload/OmniSharp.vim index 3eff7e28b..610db7692 100644 --- a/autoload/OmniSharp.vim +++ b/autoload/OmniSharp.vim @@ -106,10 +106,13 @@ endfunction function! OmniSharp#FindSolutionOrDir(...) abort let interactive = a:0 ? a:1 : 1 let bufnr = a:0 > 1 ? a:2 : bufnr('%') - if empty(getbufvar(bufnr, 'OmniSharp_buf_server')) + let cache = getbufvar(bufnr, 'OmniSharp_buf_server') + if empty(cache) || index(OmniSharp#proc#ListJobs(), cache) < 0 try let sln = s:FindSolution(interactive, bufnr) + if sln != cache call setbufvar(bufnr, 'OmniSharp_buf_server', sln) + endif catch return '' endtry @@ -247,11 +250,11 @@ endfunction function! s:FindSolution(interactive, bufnr) abort - let solution_files = s:FindSolutionsFiles(a:bufnr) - if empty(solution_files) - " This file has no parent solution, so check for running solutions - return s:FindRunningServerForBuffer(a:bufnr) + let running_server_for_buffer = s:FindRunningServerForBuffer(a:bufnr) + if !empty(running_server_for_buffer) + return running_server_for_buffer endif + let solution_files = s:FindSolutionsFiles(a:bufnr) if len(solution_files) == 1 return solution_files[0] diff --git a/autoload/OmniSharp/actions/workspace.vim b/autoload/OmniSharp/actions/workspace.vim index 4cad4ae21..44226861f 100644 --- a/autoload/OmniSharp/actions/workspace.vim +++ b/autoload/OmniSharp/actions/workspace.vim @@ -30,6 +30,41 @@ function! s:ProjectsRH(job, response) abort call OmniSharp#log#Log(a:job, 'Workspace complete: no projects') call OmniSharp#project#RegisterLoaded(a:job) endif + + let projectFolders = mapnew(projects, {_,p -> fnamemodify(p.path, ':p:h') }) + for i in filter(range(1, bufnr('$')), {_,x -> bufexists(x) && !empty(getbufvar(x, "OmniSharp_host")) && getbufvar(x, "OmniSharp_host").sln_or_dir != a:job.sln_or_dir}) + let host = getbufvar(i, "OmniSharp_host") + let filePath = fnamemodify(bufname(i), ':p') + for projectFolder in projectFolders + if stridx(filePath, projectFolder) == 0 + let host.sln_or_dir = a:job.sln_or_dir + break + endif + endfor + endfor + + if a:job.sln_or_dir =~ '\.sln$' && get(g:, 'OmniSharp_stop_redundant_servers', 0) + for runningJob in OmniSharp#proc#ListRunningJobs() + let runningJobProjects = OmniSharp#proc#GetJob(running).projects + let isCompletelyCoveredByNewestSolution = 1 + for i in range(1, len(runningJobProjects)) + let isProjectCoveredByNewestSolution = 0 + for j in range(1, len(projects)) + if runningJobProjects[i].path == projects[j].path + let isProjectCoveredByNewestSolution = 1 + break + endif + endfor + if !isProjectCoveredByNewestSolution + let isCompletelyCoveredByNewestSolution = 0 + break + endif + endfor + if isCompletelyCoveredByNewestSolution + call OmniSharp#StopServer(runningJob) + endif + endfor + endif endfunction let &cpoptions = s:save_cpo diff --git a/test/fixusings.vader b/test/fixusings.vader index 2de14c59d..b76b0557c 100644 --- a/test/fixusings.vader +++ b/test/fixusings.vader @@ -47,6 +47,7 @@ Expect cs(should add required using): Given cs(): +<<<<<<< HEAD using System; public class Test @@ -106,6 +107,8 @@ Expect cs(should add another required using): Given cs(): +======= +>>>>>>> 37fa24e (feature (sln_scope_handling): stop running job only if completely covered by newly started job. Use g:OmniSharp_stop_redundant_servers to change that default behavior) public class test { class1 ns1 = new class1(); } @@ -121,7 +124,11 @@ Given cs(): } Execute (run fix usings): +<<<<<<< HEAD call OmniSharpTestInitializeBuffer('FixUsings5') +======= + call OmniSharpTestInitializeBuffer('FixUsings3') +>>>>>>> 37fa24e (feature (sln_scope_handling): stop running job only if completely covered by newly started job. Use g:OmniSharp_stop_redundant_servers to change that default behavior) call OmniSharpWarmup('OmniSharp#actions#usings#Fix', []) call OmniSharpTestAwait('OmniSharp#actions#usings#Fix', []) @@ -141,7 +148,11 @@ Expect cs(should not add ambiguous using): } Execute (should return ambiguous usings to Callback): +<<<<<<< HEAD call OmniSharpTestInitializeBuffer('FixUsings6') +======= + call OmniSharpTestInitializeBuffer('FixUsings4') +>>>>>>> 37fa24e (feature (sln_scope_handling): stop running job only if completely covered by newly started job. Use g:OmniSharp_stop_redundant_servers to change that default behavior) call OmniSharpWarmup('OmniSharp#actions#usings#Fix', []) let g:OmniSharp_test_locations = [] function! s:CallbackFixUsings(locations) @@ -162,7 +173,11 @@ Execute (should return ambiguous usings to Callback): AssertEqual g:OmniSharp_test_locations[0].lnum, 2 Execute (should add ambiguous usings to quickfix): +<<<<<<< HEAD call OmniSharpTestInitializeBuffer('FixUsings7') +======= + call OmniSharpTestInitializeBuffer('FixUsings5') +>>>>>>> 37fa24e (feature (sln_scope_handling): stop running job only if completely covered by newly started job. Use g:OmniSharp_stop_redundant_servers to change that default behavior) call OmniSharpWarmup('OmniSharp#actions#usings#Fix', []) " Cannot use OmniSharpTestAwait, as the callback prevents the default quickfix " behaviour From 8bfa8fa6a35920b6795bb01eeaa3558dec050a1d Mon Sep 17 00:00:00 2001 From: Minh-Tam TRAN Date: Sat, 16 Oct 2021 16:17:04 +0200 Subject: [PATCH 2/6] feature (sln_scope_handling): make the option of stopping redundant servers enabled by default --- autoload/OmniSharp/actions/workspace.vim | 2 +- plugin/OmniSharp.vim | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/autoload/OmniSharp/actions/workspace.vim b/autoload/OmniSharp/actions/workspace.vim index 44226861f..24e895517 100644 --- a/autoload/OmniSharp/actions/workspace.vim +++ b/autoload/OmniSharp/actions/workspace.vim @@ -43,7 +43,7 @@ function! s:ProjectsRH(job, response) abort endfor endfor - if a:job.sln_or_dir =~ '\.sln$' && get(g:, 'OmniSharp_stop_redundant_servers', 0) + if a:job.sln_or_dir =~ '\.sln$' && get(g:, 'OmniSharp_stop_redundant_servers', 1) for runningJob in OmniSharp#proc#ListRunningJobs() let runningJobProjects = OmniSharp#proc#GetJob(running).projects let isCompletelyCoveredByNewestSolution = 1 diff --git a/plugin/OmniSharp.vim b/plugin/OmniSharp.vim index 2e1d84ba9..bb7751bfd 100644 --- a/plugin/OmniSharp.vim +++ b/plugin/OmniSharp.vim @@ -46,6 +46,8 @@ let g:OmniSharp_completion_without_overloads = get(g:, 'OmniSharp_completion_wit let g:omnicomplete_fetch_full_documentation = get(g:, 'omnicomplete_fetch_full_documentation', 1) +let g:OmniSharp_stop_redundant_servers = get(g:, 'OmniSharp_stop_redundant_servers', 1) + command! -bar -nargs=? OmniSharpInstall call OmniSharp#Install() command! -bar -nargs=? OmniSharpOpenLog call OmniSharp#log#Open() command! -bar -bang OmniSharpStatus call OmniSharp#Status(0) From 8020cba1b1af2fc852e7879edb9e34785621e818 Mon Sep 17 00:00:00 2001 From: Minh-Tam TRAN Date: Sat, 16 Oct 2021 17:57:28 +0200 Subject: [PATCH 3/6] feature (sln_scope_andling): give the user the ability to manually pick another server to handle the current file --- autoload/OmniSharp.vim | 18 ++++++++++++++++++ autoload/OmniSharp/actions/workspace.vim | 8 ++++---- ftplugin/cs/OmniSharp.vim | 3 +++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/autoload/OmniSharp.vim b/autoload/OmniSharp.vim index 610db7692..fd2be828d 100644 --- a/autoload/OmniSharp.vim +++ b/autoload/OmniSharp.vim @@ -62,6 +62,20 @@ function! OmniSharp#CompleteRunningSln(arglead, cmdline, cursorpos) abort return filter(jobs, {_,job -> job =~? a:arglead}) endfunction +function! OmniSharp#CompleteOtherRunningSlnOrDirCoveringCurrentFile(arglead, cmdline, cursorpos) abort + let slnsOrDirsCoveringCurrentFile = [] + let filePath = fnamemodify(expand('%'), ':p') + let currentlyAssignedJob = get(OmniSharp#GetHost(), 'sln_or_dir') + for runningJob in filter(OmniSharp#proc#ListRunningJobs(), {_,x -> x != currentlyAssignedJob}) + for runningJobProjectPath in mapnew(OmniSharp#proc#GetJob(runningJob).projects, "fnamemodify(v:val.path, ':p:h')") + if stridx(filePath, runningJobProjectPath) == 0 + call add(slnsOrDirsCoveringCurrentFile, runningJob) + break + endif + endfor + endfor + return filter(slnsOrDirsCoveringCurrentFile, {_,sln_or_dir -> sln_or_dir =~? a:arglead}) +endfunction function! OmniSharp#IsAnyServerRunning() abort return !empty(OmniSharp#proc#ListRunningJobs()) @@ -248,6 +262,10 @@ function! OmniSharp#RestartAllServers() abort endfor endfunction +function! OmniSharp#PickRunningServer(server) abort + let host = get(b:, 'OmniSharp_host', {}) + let host.sln_or_dir = a:server +endfunction function! s:FindSolution(interactive, bufnr) abort let running_server_for_buffer = s:FindRunningServerForBuffer(a:bufnr) diff --git a/autoload/OmniSharp/actions/workspace.vim b/autoload/OmniSharp/actions/workspace.vim index 24e895517..7c4c16159 100644 --- a/autoload/OmniSharp/actions/workspace.vim +++ b/autoload/OmniSharp/actions/workspace.vim @@ -45,12 +45,12 @@ function! s:ProjectsRH(job, response) abort if a:job.sln_or_dir =~ '\.sln$' && get(g:, 'OmniSharp_stop_redundant_servers', 1) for runningJob in OmniSharp#proc#ListRunningJobs() - let runningJobProjects = OmniSharp#proc#GetJob(running).projects let isCompletelyCoveredByNewestSolution = 1 - for i in range(1, len(runningJobProjects)) + let runningJobProjectsPaths = mapnew(OmniSharp#proc#GetJob(runningJob).projects, "fnamemodify(v:val.path, ':p:h')") + for i in range(len(runningJobProjectsPaths)) let isProjectCoveredByNewestSolution = 0 - for j in range(1, len(projects)) - if runningJobProjects[i].path == projects[j].path + for j in range(len(projects)) + if runningJobProjectsPaths[i] == projects[j].path let isProjectCoveredByNewestSolution = 1 break endif diff --git a/ftplugin/cs/OmniSharp.vim b/ftplugin/cs/OmniSharp.vim index 404a2a982..85203f189 100644 --- a/ftplugin/cs/OmniSharp.vim +++ b/ftplugin/cs/OmniSharp.vim @@ -25,6 +25,7 @@ command! -buffer -bar OmniSharpRestartServer call OmniSharp#RestartServer() command! -buffer -bar -nargs=? -complete=file OmniSharpStartServer call OmniSharp#StartServer() command! -buffer -bar OmniSharpStopAllServers call OmniSharp#StopAllServers() command! -buffer -bar -nargs=? -bang -complete=customlist,OmniSharp#CompleteRunningSln OmniSharpStopServer call OmniSharp#StopServer(0, ) +command! -buffer -bar -nargs=? -complete=customlist,OmniSharp#CompleteOtherRunningSlnOrDirCoveringCurrentFile OmniSharpPickRunningServer call OmniSharp#PickRunningServer() command! -buffer -bar OmniSharpCodeFormat call OmniSharp#actions#format#Format() command! -buffer -bar OmniSharpDocumentation call OmniSharp#actions#documentation#Documentation() @@ -68,6 +69,7 @@ nnoremap (omnisharp_go_to_definition) :OmniSharpGotoDefinition (omnisharp_highlight) :OmniSharpHighlight nnoremap (omnisharp_navigate_up) :OmniSharpNavigateUp nnoremap (omnisharp_navigate_down) :OmniSharpNavigateDown +nnoremap (omnisharp_pick_running_server) :OmniSharpPickRunningServer nnoremap (omnisharp_preview_definition) :OmniSharpPreviewDefinition nnoremap (omnisharp_preview_implementation) :OmniSharpPreviewImplementation nnoremap (omnisharp_rename) :OmniSharpRename @@ -114,6 +116,7 @@ let b:undo_ftplugin .= ' \| delcommand OmniSharpHighlightTypes \| delcommand OmniSharpNavigateUp \| delcommand OmniSharpNavigateDown +\| delcommand OmniSharpPickRunningServer \| delcommand OmniSharpPreviewDefinition \| delcommand OmniSharpPreviewImplementation \| delcommand OmniSharpRename From f76b53e1d91272385bb1e6b53bcee095930c54ef Mon Sep 17 00:00:00 2001 From: Minh-Tam TRAN Date: Sat, 16 Oct 2021 19:29:41 +0200 Subject: [PATCH 4/6] fix (sln_scope_handling): revert some merge that did not work out --- test/fixusings.vader | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/test/fixusings.vader b/test/fixusings.vader index b76b0557c..2de14c59d 100644 --- a/test/fixusings.vader +++ b/test/fixusings.vader @@ -47,7 +47,6 @@ Expect cs(should add required using): Given cs(): -<<<<<<< HEAD using System; public class Test @@ -107,8 +106,6 @@ Expect cs(should add another required using): Given cs(): -======= ->>>>>>> 37fa24e (feature (sln_scope_handling): stop running job only if completely covered by newly started job. Use g:OmniSharp_stop_redundant_servers to change that default behavior) public class test { class1 ns1 = new class1(); } @@ -124,11 +121,7 @@ Given cs(): } Execute (run fix usings): -<<<<<<< HEAD call OmniSharpTestInitializeBuffer('FixUsings5') -======= - call OmniSharpTestInitializeBuffer('FixUsings3') ->>>>>>> 37fa24e (feature (sln_scope_handling): stop running job only if completely covered by newly started job. Use g:OmniSharp_stop_redundant_servers to change that default behavior) call OmniSharpWarmup('OmniSharp#actions#usings#Fix', []) call OmniSharpTestAwait('OmniSharp#actions#usings#Fix', []) @@ -148,11 +141,7 @@ Expect cs(should not add ambiguous using): } Execute (should return ambiguous usings to Callback): -<<<<<<< HEAD call OmniSharpTestInitializeBuffer('FixUsings6') -======= - call OmniSharpTestInitializeBuffer('FixUsings4') ->>>>>>> 37fa24e (feature (sln_scope_handling): stop running job only if completely covered by newly started job. Use g:OmniSharp_stop_redundant_servers to change that default behavior) call OmniSharpWarmup('OmniSharp#actions#usings#Fix', []) let g:OmniSharp_test_locations = [] function! s:CallbackFixUsings(locations) @@ -173,11 +162,7 @@ Execute (should return ambiguous usings to Callback): AssertEqual g:OmniSharp_test_locations[0].lnum, 2 Execute (should add ambiguous usings to quickfix): -<<<<<<< HEAD call OmniSharpTestInitializeBuffer('FixUsings7') -======= - call OmniSharpTestInitializeBuffer('FixUsings5') ->>>>>>> 37fa24e (feature (sln_scope_handling): stop running job only if completely covered by newly started job. Use g:OmniSharp_stop_redundant_servers to change that default behavior) call OmniSharpWarmup('OmniSharp#actions#usings#Fix', []) " Cannot use OmniSharpTestAwait, as the callback prevents the default quickfix " behaviour From 79d08c04d5a2c938460522dff3fe780d700bddd7 Mon Sep 17 00:00:00 2001 From: Minh-Tam TRAN Date: Sat, 16 Oct 2021 19:37:22 +0200 Subject: [PATCH 5/6] fix (sln_scope_handling): attempt to fix vader tests (Vim E117: Unknown function: mapnew --- autoload/OmniSharp/actions/workspace.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/OmniSharp/actions/workspace.vim b/autoload/OmniSharp/actions/workspace.vim index 7c4c16159..8bd5b4bfa 100644 --- a/autoload/OmniSharp/actions/workspace.vim +++ b/autoload/OmniSharp/actions/workspace.vim @@ -31,7 +31,7 @@ function! s:ProjectsRH(job, response) abort call OmniSharp#project#RegisterLoaded(a:job) endif - let projectFolders = mapnew(projects, {_,p -> fnamemodify(p.path, ':p:h') }) + let projectFolders = map(copy(projects), {_,p -> fnamemodify(p.path, ':p:h') }) for i in filter(range(1, bufnr('$')), {_,x -> bufexists(x) && !empty(getbufvar(x, "OmniSharp_host")) && getbufvar(x, "OmniSharp_host").sln_or_dir != a:job.sln_or_dir}) let host = getbufvar(i, "OmniSharp_host") let filePath = fnamemodify(bufname(i), ':p') @@ -46,7 +46,7 @@ function! s:ProjectsRH(job, response) abort if a:job.sln_or_dir =~ '\.sln$' && get(g:, 'OmniSharp_stop_redundant_servers', 1) for runningJob in OmniSharp#proc#ListRunningJobs() let isCompletelyCoveredByNewestSolution = 1 - let runningJobProjectsPaths = mapnew(OmniSharp#proc#GetJob(runningJob).projects, "fnamemodify(v:val.path, ':p:h')") + let runningJobProjectsPaths = map(copy(OmniSharp#proc#GetJob(runningJob).projects), "fnamemodify(v:val.path, ':p:h')") for i in range(len(runningJobProjectsPaths)) let isProjectCoveredByNewestSolution = 0 for j in range(len(projects)) From 025a679884c182bc30958e033b1439bfb8380e80 Mon Sep 17 00:00:00 2001 From: Minh-Tam TRAN Date: Sun, 17 Oct 2021 15:00:26 +0200 Subject: [PATCH 6/6] fix (sln_scope_handling): use old map(copy()) syntax instead of mapnew() --- autoload/OmniSharp.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/OmniSharp.vim b/autoload/OmniSharp.vim index fd2be828d..6379aeee3 100644 --- a/autoload/OmniSharp.vim +++ b/autoload/OmniSharp.vim @@ -67,7 +67,7 @@ function! OmniSharp#CompleteOtherRunningSlnOrDirCoveringCurrentFile(arglead, cmd let filePath = fnamemodify(expand('%'), ':p') let currentlyAssignedJob = get(OmniSharp#GetHost(), 'sln_or_dir') for runningJob in filter(OmniSharp#proc#ListRunningJobs(), {_,x -> x != currentlyAssignedJob}) - for runningJobProjectPath in mapnew(OmniSharp#proc#GetJob(runningJob).projects, "fnamemodify(v:val.path, ':p:h')") + for runningJobProjectPath in map(copy(OmniSharp#proc#GetJob(runningJob).projects), "fnamemodify(v:val.path, ':p:h')") if stridx(filePath, runningJobProjectPath) == 0 call add(slnsOrDirsCoveringCurrentFile, runningJob) break