Skip to content

Feature Request: Add ability to change theme/grammar content type associations at runtime #81

@ainslec

Description

@ainslec

Hi,

My plugin used to dynamically be able to change file associations for my given editor type. Since using the TMPresentationReconciler, this ability appears to have been lost (conjecture on why later).

Here is my editor definition (NOTE : "my' is the associated file type, NOT 'pie') :

image

Here is the method in MySourceViewerConfiguration class:

image

Here is the TM4E definition (NOTE : "my' is the associated file type, NOT 'pie') :

image

Here is the code that I used to dynamically modify file associations to choose my editor (this previously worked before adding in the TM4E syntax highlighting) :

public void resetFileAssociations(java.util.Set<java.lang.String> extensionToAddSet) {
      IWorkbench workbench = PlatformUI.getWorkbench();
      final org.eclipse.ui.internal.registry.EditorRegistry editorReg = (org.eclipse.ui.internal.registry.EditorRegistry) workbench.getEditorRegistry();

      org.eclipse.ui.internal.registry.EditorDescriptor editor = (org.eclipse.ui.internal.registry.EditorDescriptor) editorReg.findEditor("com.consoli.myrion.eclipse.ui.editor.MyEditorId3");

      IFileEditorMapping[] originalMappings = editorReg.getFileEditorMappings();

      java.util.ArrayList<org.eclipse.ui.internal.registry.FileEditorMapping> newMappings = new java.util.ArrayList<org.eclipse.ui.internal.registry.FileEditorMapping>();

      boolean firstTime = false;

      if (originalMappings2 == null) {
         originalMappings2 = new java.util.HashSet<String>();
         firstTime = true;
      }

      int numOriginalMappings = originalMappings.length;

      for (int i = 0; i < numOriginalMappings; i++) {

         org.eclipse.ui.internal.registry.FileEditorMapping m = (org.eclipse.ui.internal.registry.FileEditorMapping) originalMappings[i];

         String extension = m.getExtension();

         IEditorDescriptor[] editors = m.getEditors();

         for (IEditorDescriptor currentEditor : editors) {
            boolean isMyEditor = currentEditor == editor;
            if (isMyEditor) {

               if (firstTime) {
                  originalMappings2.add(extension);
               } else {
                  if (!originalMappings2.contains(extension)) {
                     m.removeEditor(editor);
                  }
               }
            }
         }

         if (editors.length > 0) {
            newMappings.add(m);
         }
      }

      for (String extensionToAdd : extensionToAddSet) {
         if (!originalMappings2.contains(extensionToAdd)) {
            org.eclipse.ui.internal.registry.FileEditorMapping mapping = new org.eclipse.ui.internal.registry.FileEditorMapping(extensionToAdd);
            System.out.println("Associating " + extensionToAdd);
            mapping.addEditor(editor);
            mapping.setDefaultEditor(editor);
            newMappings.add(mapping);
         }
      }

      final FileEditorMapping[] fileEditorMappings = newMappings.toArray(new org.eclipse.ui.internal.registry.FileEditorMapping[newMappings.size()]);
      final Display default1 = Display.getDefault();
      final Runnable runnable = new Runnable() {
         @Override
         public void run() {
            editorReg.setFileEditorMappings(fileEditorMappings);
         }
      };
      default1.syncExec(runnable);
}

Now we wish to dynamically associate the 'pie' file type / suffix with the same editor as 'my'.

So we call the resetFileAssociations() method with set containing ["pie", "my"]. This worked perfectly prior to tm4e integration. It also works now actually. It associates the editor type with the new suffix, but TM4E then uses the content type to grammar map and finds it empty for "pie". Same with themes.

As you would expect from the previous description, here is what happens when I attempt to access a ".pie" (previously it worked because the previous reconciler did not require configuration):

g655

I believe that TM4E is using IGrammarRegistryManager to look up grammar / theme associations, and this cannot be modified at runtime (as far as I am aware) - therefore the .pie file suffix resolves to null. I would like to be able to alter grammar/theme associations on the fly same as is possible with the suffix to default editor associations.

The use case for this is that I have one file type that when saved, controls file associations for other file types. For example, lets say I have a file with suffix .mysettings, that file might contain a single line with a comma separated list of file suffixes such as "one, two, three". Now the builder sees when that file changes and calls the method detailed earlier. It reads the content of the file, and now dynamically associates the files with the editor (described earlier). That editor is associated with the TMPresentationReconciler. BUT, the presentation reconciler will not allow files without statically registered suffixes and themes to render. Eclipse has the functionality to change file associations at runtime, but TM4E does not (yet) (as far as I know) have the ability to render for a file suffix that has not been statically registered via the plugin.xml.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions