Closed (fixed)
Project:
Vim Plugin For Drupal
Version:
7.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Reporter:
Created:
1 Nov 2011 at 13:17 UTC
Updated:
20 Jan 2012 at 02:40 UTC
Jump to comment: Most recent file
If you edit javascript files, then you want most of the same settings there that we have for PHP.
First, I moved most of ftplugin/php_drupal.vim to a new file, ftplugin/drupal.vim. There is no drupal filetype, so this will never be :sourced automatically. Instead, I :source it from ftplugin/php_drupal.vim. Then I added ftplugin/javascript_drupal.vim; so far, it does nothing but :sourcethe new ftplugin/drupal.vim.
While I was at it, I set one more option:
:setl nojoinspaces "No second space when joining lines that end in "."
This helps if you replace some complicated expression with a single variable, giving you
$foo = 'some long string' .
$bar
and then you join the two lines. By default, vim puts two spaces after the dot, violating coding standards.
Questions:
'ignorecase' and/or 'smartcase' for javascript files.| Comment | File | Size | Author |
|---|---|---|---|
| #28 | support-js-1328290-28.patch | 1.76 KB | benjifisher |
| #26 | support-js-1328290-26.patch | 775 bytes | benjifisher |
| #15 | support-js-1328290-15.patch | 28.64 KB | benjifisher |
| #14 | support-js-1328290-14.patch | 14.95 KB | benjifisher |
| #13 | support-js-1328290-13.patch | 816 bytes | benjifisher |
Comments
Comment #1
benjifisherI apologize: I accidentally committed the patch. I just do not know git well enough yet. (But I learned something today!) I do not intentionally commit my own patches without review.
Real soon now, I will propose further changes now that I have committed the patch I wanted to, from #1139024-4: Add support for Drupal Snippets for Vim. That will simplify things.
Comment #2
benjifisherThis is a big, complicated patch, but it leads to a simpler structure for this project. It is based on the idea of introducing "drupal" as a secondary filetype as in #1139024-4: Add support for Drupal Snippets for Vim, but it implements that idea in a different way, so it starts by reversing that patch (even though I just commited it yesterday). I will walk through the changes.
ftdetect/drupal.vimso that it just does:set ft=phpfor all our drupal PHP files. Then I add the lineto
plugin/drupal.vim. Now our PHP files will have 'filetype' set to "php.drupal", and also our CSS files will have it set to "css.drupal" and so on.ftplugin/php_drupal.vimandftplugin/javascript_drupal.vim(and others never written) to:source ftplugin/drupal.vim. This will happen automatically once we set 'filetype' to "*.drupal". So I removed theftplugin/*_drupal.vimfiles and added a few lines from the oldftplugin/php_drupal.vimtoftplugin/drupal.vim. (These lines will only be used inside PHP files.)syntax/drupal.vimwill be:sourced, after the other syntax files. So I deleted the Syntax autocommands fromplugin/drupal.vimand createdsyntax/drupal.vim. For now, all this file does is add highlighting for long comments. Trailing spaces are still dealt with inplugin/drupal.vim.plugin/drupal.vimI changed the condition under which ToggleWhitespaceMatch() bails out. Now it checks whether "drupal" is part of the 'filetype'.Comment #3
benjifisherThis version makes an improvement to
syntax/drupal.vim, which highlights long comments as errors. I noticed that inthe
*/was highlighted asdrupalOverLength(linked toError) and so it was not recognized as the end of the comment. Then the following line was givenCommenthighlighting.The fix is not perfect. If you do something odd like
then the
*/will be highlighted as Comment instead of Error, but I think that is OK. I think this version works well in most cases.Comment #4
benjifisherThis version of the patch is a major rewrite and expansion. It makes use of the tags files described in #1347302: Tags files are too big., which I just added to the project. (I cannot include them in a patch here because they are too big.)
First, I moved a function definition from the ftplugin file to the plugin file, since the function needs to be defined only once.
Next, I used the plugin file (using a function and an autocommand or two) to look for the root of the Drupal installation and the .info file of the current project. If they are found, then the script stores some useful information, such as the Drupal core version, in a buffer-local array. (In Vim terminology, a Dictionary.)
I improved the
\damapping to go to the page on api.drupal.org so that it is aware of the version of Drupal core.The scripts set the environment variable
$DRUPAL_ROOTthat is used in the tags files. This lets you use all of vim's tag-search commands. For example, put the cursor on a function such asdrupal_set_message()and type<C-W>]to opendrupal/includes/bootstrap.incand jump to the definition. For more options, see:help tag-commands:help window-tagThe tags files are also used by the PHP auto-complete script that comes in the standard vim distribution. For example, you can type
drupal_seand then<C-X><C-O>to see all completions as in the screen shot below. Note the scratch buffer at the top of the page that shows the arguments of the currently selected completion. For standard PHP functions, the scratch buffer has even more information on arguments. This also works for functions (and variables, constants, etc.) defined in the current file. See:help compl-omniComment #5
kostajh commentedThis is pretty cool - I tested the functionality you outlined in #4 and it worked just fine. Nice work!
The only problem for me is that my development environment is usually set up like so:
So in that case, when editing files in the git repo, the tags aren't generated because this plugin assumes that the files will be residing within a Drupal directory.
I'm not sure how to get around this, other than possibly using drush aliases and adding an element in the alias array that indicates where the git repo for a given environment is located, and using that information to load up the tags from the relevant local Drupal codebase. But that seems like another issue to open up, and it would be good to move ahead with committing what you have now.
Comment #6
benjifisherkostajh, thanks for testing!
I do not see a problem with your setup, although maybe I need more details. If there is a problem, maybe I can tweak the method for guessing where the Drupal root is. If all else fails, I can put in a configuration option. Of course, I really want it to work without configuration.
IIUC you have something like
and client is symlinked to drupal, so the two .module files are actually the same. Or maybe you leave off the drupal/ part of the path, and client is symlinked to www.client.com.
If I have it straight, then the tags files should work as is. When vim looks at
expand('%:p')it should get one of the two paths above. It will test either~/Sites/www.client.com/drupal/or~/work/git-repos/client/and look for the magic file/directory names index.php, cron.php, sites, themes, and modules. If it finds them, then it will set $DRUPAL_ROOT to the directory it is testing. The tags files that I added to the project use this environment variable: they do not include any real directory, and they do not rely on finding a ctags executable on the user's system. (Isn't that clever? ;)It will be a problem if your git repo is not a full Drupal install. If git-repos/client/ contains only the sites/ subdirectory, then my script cannot guess where to look for DRUPAL_ROOT.
Comment #7
kostajh commentedSorry, I should have been clearer. That's the problem. I don't keep a full Drupal site (core, contrib modules etc) in version control, just the custom development.
Anyway, I don't think that should prevent this patch from being committed, it is just something to consider in the future.
Comment #8
benjifisher@kostajh:
In that case, mark this issue RTBC and I will commit the patch. Then you can open another issue and we can add other ways to find the Drupal root.
As a work-around, you can
for now.
Comment #9
kostajh commentedThanks @benjifisher!
Comment #10
jolos commentedAm I right that this is only supposed to work for modules? I can't get it to work for core, guess that's because the drupal version can't be determined.
Comment #11
benjifisher@kostajh:
You might try (using the right paths, of course)
No, that does not work. If you apply the attached patch on top of the one from #4, I think it will work.
(You already have
~/work/git-repos/client/sites, right?)Two questions: does this work? If so, is it a reasonable/useful option for you?
@jolos:
I think everything works as is for core or contributed modules and themes.
I already have a question in the comments in
ftplugin/drupal.vim:How do you like the effect of
It should work for autocompletion. If the script finds the right
$DRUPAL_ROOT, it should work pretty well for tag searches, too.Assuming we have the right
$DRUPAL_ROOT, there are other ways to determine the core version. We could check the first few lines ofCHANGELOG.txt, although people sometimes delete that for security reasons. We could also look at$DRUPAL_ROOT/modules/system/system.info. Comments?Comment #12
jolos commented@benjifisher
It works indeed for some core files, but when I start editing entity.inc, the tags file isn't found.
Comment #13
benjifisher@jolos:
Right, that is neither a module nor a theme. I am still not sure which of the options I described in #11 is most reliable, but let's try using
$DRUPAL_ROOT/modules/system/system.info. Please try the attached patch on top of the one from #4.If you are editing a file that has
/modules/or/themes/in its path, then the script setsb:Drupal_info.TYPEto 'module' or 'theme'. So far, that is only used when editing a .info file, to decide which keywords get highlighted. (I will commit that code to the master branch soon!) Who knows, we might find some other use for this information later. Maybe, if the file has/includes/in its path, we should setb:Drupal_info.TYPEto 'include'.Comment #14
benjifisherSince I committed other changes to the master branch, the patch in #4 no longer applies cleanly. Here is a revised patch. It includes the changes from #4, #11, and #13 above.
Because I am merging with other changes, and because I want feedback on the questions from #10 and #12, I am rolling the status back to "needs review."
Comment #15
benjifisherHere is a version of the patch that includes documentation updates: both README.txt and doc/drupal.txt. While I was at it, I added a section on installation with pathogen to each of these files.
Comment #16
jolos commentedI've applied the last patch and everything seems to be working, the added documentation is very welcome as well. As I can't find any error, I'm setting this to rtbc. This is a great patch and b:Drupal_info will imo be very useful for other features.
Comment #17
benjifisher@jolos:
Thanks for the review! I am looking forward to getting feature requests. I agree that the b:Drupal_info variable, and the overall structure provided by this patch, will make it easier to add new features.
I have committed the patch from #15, and I am marking this issue as fixed.
Comment #19
benjifisher@kostajh,
Does
$drush status rootgive the right answer? If so, we can use that, or parse your drushrc.php (probably faster) to set
b:Drupal_info.DRUPAL_ROOT.Comment #20
kostajh commented@benjifisher: If you are in the root of a Drupal directory, and run "drush status" you will get output like this:
So you could look at "Drupal version" there and use that to set
b:Drupal_info.DRUPAL_ROOT.The command `drush status root` does nothing.
One way you could get the Drupal root from a repository is by using Drush aliases.
Here is an example alias:
We could add a line in an alias for the path to the git repo:
The plugin could search through the aliases (you can load them all by typing "drush sa") and check to see if the current file in Vim matches the path to a git repo, then run "drush status @mysite.dev" and load Drupal version from there.
Complicated! Alternatively it would be nice to just type "set drupal_api=7" in the status line.
Comment #21
benjifisher@kostajh,
Au contraire: Perhaps you get nothing, but I get
Or I get that as one line when I just type
$drush st(In case it makes a difference, I have drush 4.5.)
If I can find the Drupal root, I can find the core version reliably from
DRUPAL_ROOT/modules/system/system.info. (Add "core/" for D8.) I think you are getting the 'CORE' and 'DRUPAL_ROOT' keys mixed up. (That is understandable, since the whole system does not work with your set-up.) Anyway, I would rather get both.For me,
drush stcauses a noticeable lag, anddrush sais worse. It would be annoying to go through that each time I open a module file.Why don't you think about coercing drush into finding your Drupal install directory, and I will think about adding a manual override. (In vim, we use :let with variables, :set with built-in options; and it is the command line, not the status line.)
Comment #22
kostajh commentedI see. I thought you meant "drush status root" run from any arbitrary location, but you are running it from within a site directory. In that case you can do
drush status "Drupal version".Comment #23
benjifisher@kostajh,
Yes, but I want the Drupal root as well as the Drupal version. I could also use
$drush status "Drupal root". I still want to know: if you do$drush status rootwith your setup, does it tell you something useful?Comment #24
kostajh commented@benjifisher: If I run
drush status rootfrom within a git repo, there is no output.Comment #25
benjifisher@kostajh,
Can you fix something in your drushrc.php so that drush can figure it out? Or if the "root" argument is problematic, then
$ drush stshould output something likenear the end.
If drush has no way to figure out your Drupal root, then we will have to do it.
The current script should be able to figure out your main Drupal version. (I assume that whatever module or theme you have comes with a proper .info file.) Does
give the right answer?
Comment #26
benjifisher@kostajh,
I made a bare module directory, a poor imitation of your git repo structure:
I find that
b:Drupal_info.COREis correctly set to 7. In particular, omni-completion works. For example, I enterdrupal_get_path<C-X><C-O>and the function template opens in a small scratch window. (See the screen shot in comment #4.)With the attached patch, I added the line
to my vimrc file. Now, when I edit the module file, the script finds an appropriate Drupal directory. It may be different from the one that is symlinked to the current git repo, but for now I do not see any disadvantage. I think the only way our scripts use DRUPAL_ROOT so far is to jump to tags. For example,
works fine for me.
If this suits your needs, I will add some documentation under
:help drupal-magicand commit the patch. (Or you could add the documentation.)Comment #27
kostajh commented@benjifisher this works! Thank you so much.
Comment #28
benjifisher@kostajh, you are welcome! Thanks for testing.
I added a few lines to
doc/drupal.txtand committed it: http://drupalcode.org/project/vimrc.git/commit/f0d2b12. The final patch is attached.I must be tired. I forgot to mention this issue number in the commit message. :-(