8

There are occasions when there is some code in a file that does not have any extension. Such files will have code written in a single language, but each such file may be written using a different language (like C, C++, assembly language, wiki markup, and HTML for example). Similarly, the problem also happens when there is a common extension in the file name, but different files use different programming languages.

The problem I want to solve is to avoid making each individual developer that has checked out a workspace from having to figure out what kind of file a source file is (there is either no extension, or the extension does not imply the source language used). To do so, the author of the file has the responsibility of putting the right magic in the file to let the file be opened properly. Since Eclipse has the ability to disable the formatter based on some embedded text in the file, I thought it should be possible to specify the file type as well. The Open With feature allows an individual developer to change how a file is interpreted, but must be done again each time a new workspace is checked out.

In Emacs or Vim, I can place magic strings at the top of the file to indicate how I want the file to be rendered within the editor. For example:

/* -*- c++ -*- */
/* vim: set ft=cpp: */

Is there some equivalent magic for Eclipse? My attempts at finding a solution constantly pointed out how to disable the formatter. Clearly, my inexperience with Eclipse is capping my Google-Fu.

Community
  • 1
  • 1
jxh
  • 69,070
  • 8
  • 110
  • 193
  • 3
    I don't think so. The Eclipse content type system does tend to start from the file extension. If you use the `Open With` context menu and select an editor it will be remembered for future editing. – greg-449 Nov 05 '13 at 09:21
  • @greg-449: Thanks for that. The motivation is to avoid making other developers from having to figure out what kind of file it is by giving the author of the file the responsibility of putting the right magic in the file to let the file be opened properly. – jxh Nov 05 '13 at 14:49
  • Are plugins an option? If so, my answer might be of help. – HQCasanova Nov 08 '13 at 01:07
  • 2
    @jxh Then why not use correct file extensions in the first place? – user694733 Nov 08 '13 at 08:25
  • 1
    @user694733: C++ header files may lack extensions. Scripts often lack extensions. Source files that are actually templates for automated code generators may use identical extensions but each for different languages. I don't think the reason for the requirement I present is particularly relevant. I could require only Emacs or Vim be used, but I am seeking a way to accommodate Eclipse. – jxh Nov 08 '13 at 08:55
  • @jxh Code generator case might be more complicated, but maybe you could fix C++ headers (if you are not working with std libs that is) and scripts to minimize the problem. It might be a big task if your broken codebase is big, but I got impression that you are willing to edit sources for this, and changing the extensions would be one step further. It would improve usability with other editing tools too, not just Eclipse. – user694733 Nov 08 '13 at 09:18
  • @user694733: I feel the point of an extensible editor is to be able to customize it to accommodate the coding practices of the team. I would find it somewhat off-putting if the coding practices of the team has to change to accommodate the editor. – jxh Nov 08 '13 at 16:32
  • This is a years old thread, but still people run into this problem and find the solution here. Like me. Thank you! – U. W. Jun 15 '20 at 07:36

3 Answers3

3

If there is no file extension, then things are a little harder. Instead of double-clicking to open the file, right-click -> Open with... -> other -> C++ editor. This will remember your choice for that file only.

ref:Quick way to set custom syntax highlighting in Eclipse

Community
  • 1
  • 1
Sach
  • 659
  • 8
  • 20
  • Thanks, this seems to be the way Eclipse wants to handle the issue as I have stated it, but it does not really address the problem I wanted to solve. For a team of programmers who are all free to choose their own editor, I want a team member to put some boiler plate text into files he/she creates that need it so that the other programmers on the team that use their own editor of choice will view the file properly. – jxh Nov 14 '13 at 21:30
2

Of all the free plugins providing Vim functionality within Eclipse that are available as of 8 November 2013, namely

...none seem to support modelines. Nevertheless, a Vrapper user asked more than a year ago if such support existed.

Note that Eclim is a special case. It might solve your problem since it allows you to use Vim as an embedded Eclipse editor. However, from within the Vim instance, Eclipse's key bindings will not work. I guess it's a trade-off between access to features and convenience. From Eclim's website:

Please be aware that the embedded vim does not behave like a standard eclipse editor. It's a separate program (vim) embedded into eclipse, so eclipse features are provided by eclim's vim plugins and not the usual eclipse key bindings, context menus, etc. For those that just want vim like key bindings in their eclipse editors, vrapper is an excellent alternative which provides exactly that.

There used to be a commercial plugin called Viable but development seems to have stalled since 2011. Also, some users report that it breaks under Juno.

Finally, although your question is about Eclipse, it turns out NetBeans does have a plugin that supports modelines. It's called jVi. Another plugin for Netbeans is VIEX, in case you're interested.

Hope that helps!

Sources:

Community
  • 1
  • 1
HQCasanova
  • 1,158
  • 1
  • 10
  • 15
  • @jxh Ya, thanks for poiting that out. I've corrected it. I've also expanded it to include the Eclim option. Hope it helps! – HQCasanova Nov 08 '13 at 10:08
  • @jxh Funny. Someone over on superuser asked a very similar question but for an entirely different reason: http://superuser.com/questions/355428/vim-modeline-equivalent-for-eclipse – HQCasanova Nov 08 '13 at 10:37
  • Thanks for the pointers. I am not really after emulating Vim. For a team of programmers who are all free to choose their own editor, I want a team member to put some boiler plate text into files he/she creates that need it so that the other programmers on the team that use their own editor of choice will view the file properly. – jxh Nov 14 '13 at 21:29
  • @jxh Thanks for clarifying. Then the only thing I can think of is the shebang line. I mean, in the absence (it seems) of an modeline-like feature in Eclipse, the bare minimum is to agree on a standard way to describe the type of the file, so that at least the corresponding extension can be unequivocally identified and manually added once the file has been checked out and opened for the first time. I know that's way too inconvenient but, on the other hand, is better than nothing. Until, that is, your team decides to develop a plugin that addresses this need. Ha, ha! Great question by the way! – HQCasanova Nov 15 '13 at 00:33
  • Yes, I think implementing a custom plug-in is the right solution, but no one has offered an answer, or suggested how one might be implemented. I was kind of hoping a simple plugin implemented in a few lines of Java would be sufficient, but the lack of answers in that direction suggest it is a much more involved project than that. – jxh Nov 15 '13 at 03:08
  • @jxh Well, I've been reading up on how to create your first plugin. See [this Vogella article](http://www.vogella.com/articles/EclipsePlugIn/article.html). It doesn't seem to be that involved. Pity I can't have a stab at it right now. Good luck! – HQCasanova Nov 15 '13 at 09:42
1

I'm not aware of any Eclipse plugin able to deal with vim-style modelines. However, AnyEdit gives you a pretty precise control over tab size and related prefs. And you can always search the marketplace.

romainl
  • 186,200
  • 21
  • 280
  • 313