How to add a new file association from a plugin - visual-studio-code

In a project I have some internal files that are just JSON files but with specific well known filenames. I would like to add file associations for these files from my visual studio code plugin so they automatically get recognized as JSON and highlighted accordingly but cannot see a way to do this from the plugin. Does anyone know a way to do this?

It turns out you can just add the necessary associations to the workspace settings
// add file associations for our configurations files
let conf = vscode.workspace.getConfiguration('files');
let assocs = conf.get<{}>('associations');
assocs['special.info'] = 'json';
conf.update('associations', assocs, ConfigurationTarget.Workspace);

Related

Exclude specific directories from being parsed by intellisense

I am using php-intellisense extension for my Visual Studio Code.
How do I exclude other folders from being parsed by this extension? As of now it only excludes node_modules and vendor folder.
The extension does not seem to have any specific setting so, unless I'm missing something, the only way to accomplish that is the files.exclude directive. It should definitively work with all languages because it basically makes the file or directory totally disappear from the program.
Beware though of the consequences: you won't even see the folder in the file explorer, nor will it show in searches.
There is an opened issue on the author's github. I've just added a comment to explain how to workaround it.
Please have a look to my comment: https://github.com/felixfbecker/php-language-server/issues/159#issuecomment-514581602
In brief, you can change the way the workspace files are scanned in this file :
C:\Users\USER\ .vscode\extensions\felixfbecker.php-intellisense-xxxx\vendor\felixfbecker\language-server\src\ Indexer.php
public function index(): Promise
{
return coroutine(function () {
// Old code using the rootPath
//$pattern = Path::makeAbsolute('**/*.php', $this->rootPath);
// My new pattern
$pattern = Path::makeAbsolute('**/*.php', 'C:/Users/[USER]/Projects/sources/app/code');
$uris = yield $this->filesFinder->find($pattern);
// ...
});
}
Restart VS Code after saving the changes and it will only index the needed path.

Filter file system file selection dialog by ContentTypeId

In the Eclipse plugins we're developing, we've defined a custom content type, using the org.eclipse.core.contenttype.contentTypes extension point. We're successfully using the content type to enable or disable UI components, based on, e.g., whether the user is editing a file of that type.
I'd like to take this ones step further and also use it to filter files in a file selection dialog, such that it only shows files that match the content type.
I have found that filtering a JFace Viewer this way is possible, so for files in the workspace, we could use an ElementTreeSelectionDialog and add a ViewerFilter.
Is there a way to do the same for a file selection dialog of the entire file system (instead of filtering by file extension)? Or is this impossible because it's restricted to the OS's filtering?
The standard SWT FileDialog can only be filtered by file extension and cannot be extended.
You could write your own file selection dialog using the normal Java File or Path APIs with a tree viewer and a viewer filter.
Because the files are outside of the workspace you can't use any of the IResource, IFile, IFolder APIs. However you can still use the IContentTypeManager interface which gives you access to your content types.
IContentTypeManager manager = Platform.getContentTypeManager();
If the file extension is enough to distinguish the files you can just use:
IContentType contentType = manager.findContentTypeFor("the file name");
If you need to use the content describers use:
InputStream stream = ... new FileInputStream(....
IContentType contentType = manager.findContentTypeFor(stream, "the file name");
stream.close();

How to specify and read properties in an Eclipse plugin

I have an Eclipse product which uses my own plugins. I want to read some properties based on user inputs. I want to persist these properties on some user action, and read those properties back when required. Can this be achieved using some Eclipse API?
A more elaborate description of the above problem:
Say I have a property abc=xyz in a config file myconfig.ini. This property is read by the perspective during the bootstrapping process. During use of the perspective, some action sets this property to a new value xyz=def. Now, I should be able to save the new value in myconfig.ini. So next time the bootstrapping happens, the value of xyz is read as def instead of abc. However, I can also choose to manually set it to abc by editing the myconfig.ini file.
How would I manage myconfig.ini? Where should it exist within my eclipse product project?
What is the best API to manage reading, writing and updating properties in myconfig.ini?
You can use resource markers mechanism:
IMarker marker = file.createMarker(IMarker.MARKER);
marker.setAttribute(IMarker.MESSAGE, "blabla");
marker.setAttribute("attr", 5);
You can search for markers by using the findMarkers methods on IResource.
See FAQ also
You should consider using the apache configurations API http://commons.apache.org/proper/commons-configuration/
It can read and write INI files and if you want to change the configuration file type or add more configuration options you can simply configure it.
I would add a hidden directory to the workspace root e.g. ${WORKSPACE}/.productName/product.ini
and add an ISaveParticipant that ensures the ini file gets updated on shutdown.
You can get the Workspace using the ResourcesPlugin
IWorkspace workspace = ResourcesPlugin.getWorkspace();
and resolve it to an absolute path
IWorkspaceRoot wsRoot = workspace.getRoot();
IPath wsPath = wsRoot.getRawLocation();
IPath absoluteWsPath = wsPath.makeAbsolute();

How to hijack files from clearcase from eclipse plugin

I am developing a component which generates code based on templates inside java class. The project use clearcase as SCM. After the code update, the files are in read-only state. If i am adding anything to any java class, i have to make it hijack and paste the source code templates inside the class. Let's suppose the jAutoDoc plugin which is used for adding comment. If user select a class, click on generate comment. The comment will not paste if the file is not in write mode.
Clearcase Plugin Vendor : IBM Rational.
Eclipse Version : 3.5
Please help. Is there any way to do hijack a file from java code?
Thanks in advance..
Thanks VonC.
For making a java file in write mode through eclipse JDT API. This method will set the preference "read only" of the resource to "false".
private static void setCompilationUnitWriteMode(ICompilationUnit cu) throws CoreException {
ResourceAttributes resourceAttributes = cu.getResource().getResourceAttributes();
if (resourceAttributes != null) {
// Setting Writemode true
resourceAttributes.setReadOnly(false);
cu.getResource().setResourceAttributes(resourceAttributes);
}
}
For Non Java Resource
First create the IFile object, set the preference "read only" of the resource to "false".
IFile file = path.getFile()
file.getFile().getResourceAttributes();
if (resourceAttributes != null) {
resourceAttributes.setReadOnly(false);
file.setResourceAttributes(resourceAttributes);
}
Hijacking files only means making them read - write through an OS-based operation. (for snapshot view only, not dynamic ones)
The question is though: do you need to version your classes completed by your plugin?
Because in that case, a cleartool checkout is more appropriate.
Otherwise, read write is enough, changing file attribute through java.

Is there a ctrl-click through to resource bundles in Eclipse?

If I have a Java source file open in Eclipse I want to be able to click on a resource bundle key to open the resource bundle at the associated line for editing. For example if I had a bundle Bundle_en.properties:
key1=Value One
key2=Value Two
And a source file:
ResourceBundle bundle = ResourceBundle.getBundle("Bundle", Locale.ENGLISH);
String value = bundle.getString("key1");
I would like to be able to ctrl-click on that "key1" in the source file and have the properties file open up with key1 highlighted. In a real world scenario I would have a dozen properties files each containing hundreds of keys accessed from 20 different classes. Any pointers appreciated.
Thank you!
I'm surprised you can't just CTRL+click the variable in the getString method.
I tested this in Eclipse 3.4.1
1) create a class containing:
private static final String FOO = "bar";
2) right click FOO and select Source -> Externalize Strings
3) select the defaults and you'll end up with a messages.properties file and a Messages class that contains the ResourceBundle referencing code linking FOO to the value in messages.properties.
Once it's created FOO will look like:
static final String FOO=Messages.getString("Utils.0"); //$NON-NLS-1$
When I CTRL+click on "Utils.0" it takes me directly to the property in the resource bundle.
I am not sure eclipse provide this feature in a direct way.
However, you should be able to test your language packs associated with your plugin with an eclipse tool like Babel.
See how JBoss does it.
(source: jboss.org)