Repository navigation
[WIP] Associate each php buffer with some project #959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
15f4b47
c6c6be1
e4073cd
6366348
5a23450
1ba95c1
a350f43
d185c66
ffc7e75
6fff43e
868cfdb
012e445
7d84b30
4091887
9dc4f93
b07109b
bb9017b
e12d165
0113d9e
1c78674
f2ea242
7446a9f
29c0481
54ae848
2d71ade
7a11464
d3df431
c0f13e6
15dc14e
8abe37f
ea568c1
c3d1583
0f07146
5e0b9fb
6b7cd01
73b65bc
f5df4f6
e008e14
128e4d8
5d8bf3f
e3b4386
abfbc2c
2cf71ef
111598a
88fbf57
027532b
32ca70b
ac37012
3224c65
30a7a98
b80e67e
f0a2f27
f2c9582
cc700e9
df90934
9d5c6a7
ec932c6
82f9aaa
0ff3e70
95d85ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -459,49 +459,13 @@ endfunction | |||||
| " RPC -->-->-->-->-->-- | ||||||
| """"""""""""""""""""""" | ||||||
|
|
||||||
| function! s:searchDirectoryUpwardForRootPatterns(initialDirectory, workspaceRootPatterns, fallbackDirectory) | ||||||
| if index(g:phpactorGlobalRootPatterns, '/') < 0 | ||||||
| call add(g:phpactorGlobalRootPatterns, '/') | ||||||
| endif | ||||||
|
|
||||||
| let l:directory = a:initialDirectory | ||||||
|
|
||||||
| while index(g:phpactorGlobalRootPatterns, l:directory) < 0 | ||||||
| if s:directoryMatchesToPatterns(l:directory, a:workspaceRootPatterns) | ||||||
| return l:directory | ||||||
| endif | ||||||
|
|
||||||
| let l:directory = fnamemodify(l:directory, ':h') | ||||||
| endwhile | ||||||
|
|
||||||
| if index(g:phpactorGlobalRootPatterns, l:directory) >= 0 | ||||||
| let l:directory = a:fallbackDirectory | ||||||
| endif | ||||||
|
|
||||||
| return l:directory | ||||||
| endfunction | ||||||
|
|
||||||
| function s:directoryMatchesToPatterns(directory, patterns) abort | ||||||
| for l:pattern in a:patterns | ||||||
| if (filereadable(a:directory .'/'. l:pattern)) | ||||||
| return v:true | ||||||
| endif | ||||||
| endfor | ||||||
|
|
||||||
| return v:false | ||||||
| endfunction | ||||||
|
|
||||||
| function! phpactor#rpc(action, arguments) | ||||||
| " Remove any existing output in the message window | ||||||
| execute ':redraw' | ||||||
|
|
||||||
| let request = { "action": a:action, "parameters": a:arguments } | ||||||
|
|
||||||
| let l:workspaceDir = empty(g:phpactorProjectRootPatterns) ? g:phpactorInitialCwd : s:searchDirectoryUpwardForRootPatterns( | ||||||
| \ fnamemodify(phpactor#_path(), ':h'), | ||||||
| \ g:phpactorProjectRootPatterns, | ||||||
| \ g:phpactorInitialCwd | ||||||
| \) | ||||||
| let l:workspaceDir = (get(b:, 'phpactorProject', v:null) isnot v:null) ? b:phpactorProject.primaryRootPath : g:phpactorInitialCwd | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will solve the issue #985 It should not be phpactor responsibility to resolve the directory in the first place, we should only ensure that we provide the root directory when sending a request. I mentioned it because on this line I think we should consider the all root detection mechanism as external.
So instead of coupling this piece of code to any system, let's couple it to an abstract strategy that could be defined by anyone.
Suggested change
With if !exists('g:PhpactorRootDirectoryStrategy')
let g:PhpactorRootDirectoryStrategy = {-> g:phpactorInitialCwd}
endif
function! phpactor#getRootDirectory() abort
let l:Strategy = get(b:, 'PhpactorRootDirectoryStrategy', g:PhpactorRootDirectoryStrategy)
if type(function('type')) != type(l:Strategy)
let l:Strategy = {-> g:phpactorInitialCwd}
endif
return l:Strategy()
endfunctionAt this point the user is free to define both global and buffer local strategies. To use with your system" vimrc
let g:PhpactorRootDirectoryStrategy = {-> get(get(b:, 'phpactorProject', {}), 'primaryRootPath', g:phpactorInitialCwd)}To use vim pwd (for users with vim-rooter for instance)" vimrc
let g:PhpactorRootDirectoryStrategy = function('getcwd')The goal of this proposal is not to diminish your work at all, it's simply to provide a way of integrating in a extensible way.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept such idea (without any implementation) as a backup in case of non-acceptance the whole work, but it should be the default approach - not backup. Your proposition sounds reasonable: gives an abstraction that contains a minimal implementation and can be replaced by any implementation maintained independently in some separate project. The whole implementation of this approach is yours so please make PR that closes this after merge. @dantleech @elythyr thanks for a lot of critical word.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks so much for all your work so far :) Even if this PR isn't merged as-is there is lots of valuable stuff here which we can take forward (especially with the Vader tests). I've found it quite difficult to follow everything going on here, but I think we could do something like:
Either as part of this PR or new PR(s) - would that cover everything?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let this PR die in the current form and (before) implement @elythyr's proposition in a separate one. @elythyr would you like to make such PR? I can do it just as well.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do it if you want. Let me know if you won't have the time.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be unpredictable so we can assume I have not. |
||||||
|
|
||||||
| let l:cmd = g:phpactorPhpBin . ' ' . g:phpactorbinpath . ' rpc --working-dir=' . l:workspaceDir | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| function! phpactor#fileutils#searchUpwardBySingleMarker(path, marker) abort | ||
| let l:markerFile = findfile(a:marker, fnamemodify(a:path, ':p:h').';') | ||
|
|
||
| return empty(l:markerFile) ? v:null : fnamemodify(l:markerFile, ':p:h') | ||
| endfunction | ||
|
|
||
| function! phpactor#fileutils#searchDirectoryUpwardByRootMarkers(initialDirectory, workspaceRootMarkers, forbiddenDirs) | ||
| for l:marker in a:workspaceRootMarkers | ||
| let l:markerPath = phpactor#fileutils#searchUpwardBySingleMarker(a:initialDirectory, l:marker) | ||
|
|
||
| if v:null is l:markerPath | ||
| continue | ||
| endif | ||
|
|
||
| if empty(a:forbiddenDirs) || ! phpactor#fileutils#containsDirectoryFrom(l:markerPath, a:forbiddenDirs) | ||
| return l:markerPath | ||
| endif | ||
| endfor | ||
|
|
||
| return v:null | ||
| endfunction | ||
|
|
||
| function! phpactor#fileutils#normalizePath(path) abort | ||
| let l:modifiers = isdirectory(a:path) ? ':p:h' : ':p' | ||
|
|
||
| return simplify(fnamemodify(a:path, l:modifiers)) | ||
| endfunction | ||
|
|
||
| function! phpactor#fileutils#containsDirectoryFrom(directory, dirList) abort | ||
| let l:directory = phpactor#fileutils#normalizePath(a:directory) | ||
|
|
||
| for l:subdir in a:dirList | ||
| let l:subdir = phpactor#fileutils#normalizePath(l:subdir) | ||
| if phpactor#fileutils#isSubdir(l:subdir, l:directory) | ||
| return v:true | ||
| endif | ||
| endfor | ||
|
|
||
| return v:false | ||
| endfunction | ||
|
|
||
| function! phpactor#fileutils#isSubdir(expectedSubdir, directory) abort | ||
|
przepompownia marked this conversation as resolved.
Resolved
|
||
| let l:expectedSubdir = phpactor#fileutils#normalizePath(a:expectedSubdir) | ||
| let l:directory = phpactor#fileutils#normalizePath(a:directory) | ||
|
|
||
| return stridx(l:expectedSubdir, l:directory) == 0 | ||
| endfunction | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| function! phpactor#project#assigner#assignProjectToBuffer(filename, allowReassign, allowInteractive) | ||
| if !exists('g:loaded_phpactor') | ||
| return | ||
| endif | ||
|
|
||
| if exists('b:phpactorProject') && (v:false is a:allowReassign) | ||
| return | ||
| endif | ||
| let l:project = g:phpactorProjectAssigner.resolveProjectForFile(a:filename, a:allowInteractive) | ||
| if v:null is l:project | ||
| return | ||
| endif | ||
| let b:phpactorProject = l:project | ||
|
|
||
| if g:phpactorProjectAssigner.repository.hasProject(b:phpactorProject) | ||
| return | ||
| endif | ||
|
|
||
| call g:phpactorProjectAssigner.repository.addProject(b:phpactorProject) | ||
| echomsg printf('Project with root "%s" has been created.', b:phpactorProject.primaryRootPath) | ||
| endfunction | ||
|
|
||
| function! phpactor#project#assigner#create( | ||
| \ projectRepository, | ||
| \ projectRootMarkers, | ||
| \ forbiddenProjectRoots, | ||
| \ initialCwd, | ||
| \ noninteractiveResolverNames | ||
| \) abort | ||
| let l:forbiddenProjectRoots = a:forbiddenProjectRoots | ||
| if index(l:forbiddenProjectRoots, '/') < 0 | ||
| call add(l:forbiddenProjectRoots, '/') | ||
| endif | ||
|
|
||
| let l:resolverNames = filter(copy(a:noninteractiveResolverNames), function('s:isValidResolverName')) | ||
| if (index(l:resolverNames, 'initialCwd')) < 0 | ||
| call add(l:resolverNames, 'initialCwd') | ||
| endif | ||
|
|
||
| return { | ||
| \ 'initialCwd': a:initialCwd, | ||
| \ 'projectRootMarkers': a:projectRootMarkers, | ||
| \ 'forbiddenProjectRoots': l:forbiddenProjectRoots, | ||
| \ 'repository': a:projectRepository, | ||
| \ 'resolveProjectForFile': function('s:resolveProjectForFile'), | ||
| \ 'noninteractiveResolverNames': l:resolverNames | ||
| \ } | ||
| endfunction | ||
|
|
||
| function s:isValidResolverName(index, resolverName) abort | ||
| let l:validProjectResolverNames = ['manual', 'rootMarkers', 'initialCwd'] | ||
|
|
||
| return index(l:validProjectResolverNames, a:resolverName) >= 0 | ||
| endfunction | ||
|
|
||
| function s:resolveProjectForFile(file, allowInteractive) dict abort | ||
| let l:project = l:self.repository.findProjectContainingFile(a:file) | ||
|
|
||
| if type(l:project) == type({}) | ||
| return l:project | ||
| endif | ||
|
|
||
| " see phpactor#_path | ||
| let l:initialDirectory = fnamemodify(a:file, ':p:h') | ||
| let l:rootDirByMarker = phpactor#fileutils#searchDirectoryUpwardByRootMarkers( | ||
| \ l:initialDirectory, | ||
| \ l:self.projectRootMarkers, | ||
| \ l:self.forbiddenProjectRoots | ||
| \ ) | ||
|
|
||
| let l:resolvers = { | ||
| \ 'initialCwd': { -> l:self.initialCwd }, | ||
| \ 'manual': function('input', ['Enter file path: ', l:initialDirectory, 'file']) | ||
| \ } | ||
|
|
||
| if v:null != l:rootDirByMarker | ||
| let l:resolvers['rootMarkers'] = { -> l:rootDirByMarker } | ||
| endif | ||
|
|
||
| if v:false is a:allowInteractive | ||
| return s:resolveNoninteractive(l:resolvers, l:self.noninteractiveResolverNames) | ||
| endif | ||
|
|
||
| let l:choices = [ | ||
| \ { | ||
| \ 'action': l:resolvers['initialCwd'], | ||
| \ 'message': s:printHeader(a:file, l:self.initialCwd) | ||
| \ }, | ||
| \ { | ||
| \ 'action': l:resolvers['manual'], | ||
| \ 'message': printf('manual (default "%s")', l:initialDirectory) | ||
| \ } | ||
| \ ] | ||
|
|
||
| if v:null != l:rootDirByMarker | ||
| let l:item = { | ||
| \ 'action': l:resolvers['rootMarkers'], | ||
| \ 'message': 'autodetected by root marker: '.l:rootDirByMarker | ||
| \ } | ||
| call add(l:choices, l:item) | ||
| endif | ||
|
|
||
| let l:choice = 0 | ||
| while index(range(1, len(l:choices)-1), l:choice) < 0 | ||
| try | ||
| let l:choice = inputlist(map(copy(l:choices), { | ||
| \ number, item -> (number ? printf('%d. ', number) : '') . item['message'] | ||
| \ })) | ||
| catch /^Vim:Interrupt$/ | ||
| break | ||
| endtry | ||
|
|
||
| if l:choice == 0 | ||
| break | ||
| endif | ||
|
|
||
| redraw | ||
| endwhile | ||
|
|
||
| let l:selectedDir = l:choices[l:choice]['action']() | ||
|
|
||
| if v:null != l:selectedDir | ||
| let l:project = phpactor#project#project#createFromRootPath(l:selectedDir) | ||
|
|
||
| return l:project | ||
| endif | ||
| endfunction | ||
|
|
||
| function s:resolveNoninteractive(resolvers, resolverNames) abort | ||
| for l:resolverName in a:resolverNames | ||
| if v:null is get(a:resolvers, l:resolverName, v:null) | ||
| continue | ||
| endif | ||
|
|
||
| let l:primaryRootPath = a:resolvers[l:resolverName]() | ||
|
|
||
| if l:primaryRootPath !=# '' | ||
| return phpactor#project#project#createFromRootPath(l:primaryRootPath) | ||
| endif | ||
| endfor | ||
| endfunction | ||
|
|
||
| function s:printHeader(filePath, initialCwd) abort | ||
| " heredoc are available since vim 8.1.1354 | ||
| return printf(join([ | ||
| \ 'Phpactor [RPC]', | ||
| \ 'There is no project enabled for "%s" yet.', | ||
| \ 'Select the way of assign the project root', | ||
| \ 'If cancelled, "%s" will be used.' | ||
| \], "\n"), a:filePath, a:initialCwd) | ||
| endfunction |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| function! phpactor#project#project#createFromRootPath(primaryRootPath) abort | ||
| return { | ||
| \ 'primaryRootPath': s:normalizeRootPath(a:primaryRootPath), | ||
| \ 'containsFile': function('s:containsFile') | ||
| \ } | ||
| endfunction | ||
|
|
||
| function! s:containsFile(filepath) dict | ||
| let l:path = simplify(fnamemodify(a:filepath, ':p:h')) | ||
|
|
||
| return phpactor#fileutils#isSubdir(l:path, l:self.primaryRootPath) | ||
| endfunction | ||
|
|
||
| function s:normalizeRootPath(path) abort | ||
| let l:path = phpactor#fileutils#normalizePath(a:path) | ||
|
|
||
| " @todo better check if it is not an existing directory | ||
| if ! isdirectory(l:path) | ||
| throw printf('Path "%s" does not exist or is not a directory so it cannot be a root path.', l:path) | ||
| endif | ||
|
|
||
| return l:path | ||
| endfunction |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| function! phpactor#project#repository#create() abort | ||
| return { | ||
| \ 'projects': {}, | ||
| \ 'isEmpty': function('s:isEmpty'), | ||
| \ 'addProject': function('s:addProject'), | ||
| \ 'findProjectContainingFile': function('s:findProjectContainingFile'), | ||
| \ 'hasProjectWithPrimaryRoot': function('s:hasProjectWithPrimaryRoot'), | ||
| \ 'hasProject': function('s:hasProject') | ||
| \ } | ||
| endfunction | ||
|
|
||
| function! s:findProjectContainingFile(file) dict abort | ||
| for l:rootPath in reverse(sort(keys(l:self.projects))) | ||
| let l:project = l:self.projects[l:rootPath] | ||
| if l:project.containsFile(a:file) | ||
| return l:project | ||
| endif | ||
| endfor | ||
|
|
||
| return v:null | ||
| endfunction | ||
|
|
||
| function! s:addProject(project) dict abort | ||
| " todo check type | ||
| if v:null isnot get(l:self.projects, a:project.primaryRootPath, v:null) | ||
| throw printf('Project "%s" already exists', a:project.primaryRootPath) | ||
| endif | ||
|
|
||
| let l:self.projects[a:project.primaryRootPath] = a:project | ||
| endfunction | ||
|
|
||
| function! s:hasProject(project) dict abort | ||
| return get(l:self.projects, a:project.primaryRootPath, v:false) isnot v:false | ||
| endfunction | ||
|
|
||
| function! s:hasProjectWithPrimaryRoot(path) dict abort | ||
| return get(l:self.projects, a:path, v:false) isnot v:false | ||
| endfunction | ||
|
|
||
| function! s:isEmpty() dict abort | ||
| return empty(l:self.projects) | ||
| endfunction |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| #!/usr/bin/env sh | ||
| vim -Nu config/travis/.vimrc -c 'Vader! tests/VimPlugin/*' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should improve that, the goal is that it should be understandable by someone not knowing how everything works.
It's an help to show the user how the plugin will behave and hint on how to configure it if needed.
So first, I would add an information stating which kind of root detection is used.
Then you could add one function per type of detection, each one will have the responsibility to add relevant information.
For instance, we should not bother someone with the
Fbeing part ofshortmessageor not if it does not impact his current configuration.You should also use the
{advice}parameter forhealth#report_warnandhealth#report_errorin order to explain why theFoption is problematic and only in the case of the manual detection.