3

I have found that using the Eclipse "external tools" launch configuration is the best way to open my favorite editor on the current file in Eclipse, notably offering a one-time and super-flexible configuration versus the "Open With" menu, which I had tried to use beforehand and always found wanting, constraining, limiting, and requiring repetitive work for many file types. I set up this external tool as follows and this works great for files in my project:

  • Run:External Tools:External Tools Configurations...
  • New launch configuration
  • On the "Build" tab, un-check "Build before Launch"
  • On the Common tab, check "External Tools" under the "Display in favorites menu"
  • On the Main tab, enter "/usr/bin/myeditor" for the Location
  • Under "Working Directory" I am able to leave this blank
  • Under arguments ${resource_loc}
  • (At the top, in the "Name" field whatever I want the name to show up as)

I originally found this general technique for launching an external editor right here on stackoverflow.

So you can see ${resource_loc} or ${resource_path} is the one and only variable I need for this to work, and it does work great when I open a file located inside my Eclipse project and click the external tool. That part is fine.

But the problem I have just discovered occurs when I open a file that is not specifically listed in my project and then click the external tool button. It no longer works and I get an error dialog saying: "Variable references empty selection: ${resource_loc}."

Specifically, to reproduce this, I can create a new C++ "Hello World" project (CDT is included in my Eclipse along with Java) then add the line #include "/tmp/whatever.h" at the top of the one cpp file in the project and then right-click on "/tmp/whatever.h" and use the command "Open Declaration" and Eclipse kindly opens the file /tmp/whatever.h. Finally, I invoke my external tool intending to view whatever.h in my editor, and bang, that's when the problem occurs. (Before doing this I put a harmless line of commented text into /tmp/whatever.h.)

Before some SOer asks, no I am not actually trying to #include "/tmp/whatever.h". I am using this #include example as a specific way to show how the error can be reproduced. If you must know, in my case I actually observed the error when trying to open the editor while using Eclipse to browse framework files, files that are not part of my project.

I have already tried ${resource_path} to see if maybe that might have something in it in this context, but same error, and I have looked at the documented list of Eclipse launch variables here and again I don't see anything that looks like it would be more well-defined in the context of a file that is not part of the current project.

My assessment right now is that this is an unsolvable problem and likely just a limitation of trying to use the "External Tools" for this editor launching business. I only settled on that after using the default "Open With" menu in Eclipse for a while and finding the whole setup there highly unsatisfactory and requiring repeated configuration and actually IIRC it just would not work at all I believe in the context of my editor and controlling how many windows are created, whether one or many or one per project etc. Above, I simplified the launch for demonstration purposes.

OS: GNU/Linux Xubuntu 15.10.

Eclipse: Mars.1 Release (4.5.1) Build id: 20150924-1200

Community
  • 1
  • 1
user62177541
  • 368
  • 3
  • 14
  • 2
    It is not so much in the current project as in the current Workspace. I don't think any of the variable resolution works for objects outside of the workspace. – greg-449 Jan 28 '16 at 17:29
  • 1
    So you don't see any workaround? Isn't it rather odd that there's not a variable among all of them representing the file that's currently open and frontmost in the editor, be it in a project or not? The external tools configuration I have described is not connected to the project of course; it is global and across all projects. – user62177541 Jan 28 '16 at 21:11

1 Answers1

0

You can circumvent the "Workspace is the top-most Eclipse place" rule by linking files (or folders for that matter) in a project.

Example:

  1. Create a new project:

    FileNewProjectGeneralProjectNextProject name: LinkProject → Finish

  2. Create a file outside-workspace anywhere outside your Eclipse workspace dir.

  3. Import this file as link:

    Select LinkProject → FileImport...GeneralFile SystemNext >From directory: <as chosen for the file above> → Check ☑️ outside-workspace→ (⚠️ The next can be overlooked easily!) Advanced >> → Check ☑️ Create links in workspaceFinish

NB: I doesn't matter whether you uncheck  ☐ Create link locations relative to: PROJECT_LOC     ˅  or what you select from the list. ${resource_loc} is always the same. Just the file's/folder's PropertiesLocation: changes. There is a new property for linked files/folders: Resolved location which shows what ${resource_loc} is resolved to: an FQFN.

And, once you linked a file/folder in your project it is part of your project and you can also access it relatively, e.g. with ${workspace_loc:/LinkProject/outside-workspace}' which resolves to the FQFN, too.

See also Eclipse Platform User Guide – Linked resources.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107