Great idea, a universal script to locate project.
Emacs has a plugin named `projectile` which is definitely the "universal
script" for emacs,
It has been widely used and many other emacs plugins (helm / ivy / rails /
...) use it to
retrive project location of the current file.
Here I present a simple one, `projectile.vim`:
" root markers used to locate project
if !exists('g:projectile#marker')
let g:projectile#marker = '.svn,.git,.projectile'
endif
" returns nearest parent directory contains one of the markers
function! projectile#find_root(name)
let name = fnamemodify((a:name != '')? a:name : bufname(), ':p')
let finding = ''
" iterate all markers
for marker in split(g:projectile#marker, ',')
if marker != ''
" search as a file
let x = findfile(marker, name . '/;')
let x = (x == '')? '' : fnamemodify(x, ':p:h')
" search as a directory
let y = finddir(marker, name . '/;')
let y = (y == '')? '' : fnamemodify(y, ':p:h:h')
" which one is the nearest directory ?
let z = (strchars(x) > strchars(y))? x : y
" keep the nearest one in finding
let finding = (strchars(z) > strchars(finding))? z : finding
endif
endfor
return (finding == '')? '' : fnamemodify(finding, ':p')
endfunc
" returns nearest parent directory contains one of the markers
" if matching failed, returns the directory containing the file
function! projectile#find_home(name)
let name = fnamemodify((a:name != '')? a:name : bufname(), ':p')
let path = projectile#find_root(name)
if path != ''
return path
else
return isdirectory(name)? name : fnamemodify(name, ':p')
endif
endfunc
Sorry I don't know how to handle `!` in your proposal, but it is simple
enough.
It can be used to perform a project-wide grep:
" project-wide grep
function! projectile#grep(what)
let home = projectile#find_home('') " get the project home of current
file
if home != ''
exec "vimgrep /" . a:what . "/g " . fnameescape(home) . '**/*'
endif
endfunc
or press F9 to return project home:
" goto project home
function! projectile#go_home()
let home = projectile#find_home('')
if home != ''
exec "e " . fnameescape(home)
endif
endfunc
noremap <F9> :call projectile#go_home()<cr>
here is the gist:
https://gist.github.com/skywind3000/690618557f22de7aaab9aa0226b7edef
For old vim compatibility, if a new plugin using projectile.vim want to
work in
old vims, then can just copy these two function into their own source, not
hard.
Is it simple enough ? Could it be shipped with Vim now ?
在 2019年12月21日星期六 UTC+8上午2:53:24,Gary Johnson写道:
>
> On 2019-12-20, skywind3000 wrote:
> > Obviously, there are project related problems needs to be solved. At
> least we
> > have consensus below:
> >
> > 1. use something which seems to have been established as standard.
> > 2. which is easy to be implemented by plugins with minimal dependency.
> >
> > editorconfig seems like a good choice, but only one thing, there can by
> > multiple `.editorconfig` file
> > in one project.
> >
> > And you need to parse the `.editorconfig` to check if there is a `root =
> true`
> > in it, without actually
> > parsing one `.editorconfig` file, the project tree cannot be located.
> >
> > It is not simple to implemented by plugins without introducing new
> dependency
> > (the `EditorConfig` plugin).
> >
> > So, let's find some alternatives.
> >
> > OK, here is a new one:
> >
> > The vscode's `.vscode` folder
> >
> > 1. it can be used for both locating project tree and storing
> project-specific
> > configs,
> > 2. it is well known and nearly a standard.
> > 3. it is easy to be implement by plugins respectively without any
> dependency
> > and can also run in old vims.
> >
> > how about this ??
>
> Using .vscode would probably work great for users of Visual Studio
> Code. I do almost all my development on Linux, never use Visual
> Studio and had to look up what .vscode was. .vscode is not well
> known by other than VSC users. This is a good example of why and
> how plugins are a good solution for this. VSC users can use
> a plugin that looks for .vscode directories; git users can use
> a plugin that looks for .git directories; etc.
>
> Another consideration is that some of us use Vim for a lot of little
> editing tasks, not just long project editing sessions, and we
> wouldn't want it to spend a lot of start-up time determining
> whether or not it's in a project environment.
>
> On a more positive note: A common idea here seems to be looking for
> a certain file name in the parent directory of the current file or
> above it in the directory hierarchy. If Vim can do such a search
> internally quickly, it might work to have an option that lists the
> file names to be searched for and by some notation such as an
> appended '!' indicate whether Vim is to stop its search at the first
> or last such name found and declare that directory to be the project
> directory. Vim's findir() and findfile() functions already do most
> of that work. That still leaves the problem of using that directory
> name to find Vim's configuration for that project.
>
> Someone could try to write a universal project-detection plugin
> using these ideas and if it truly is universal, propose that it be
> added to Vim's standard plugin set or incorporated into Vim's code
> internally. Until we have such a working model, I think it's
> premature to add anything to Vim's internal code.
>
> Regards,
> Gary
>
>
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/vim_dev/16e22fcf-f99b-4839-a8b0-45b22a90db1c%40googlegroups.com.