Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LSP/Textmate: Implement multiview support for generic/textmate based syntax support #8080

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ide/lsp.client/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@
<specification-version>1.50</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.core.multiview</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>1.71</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.libs.flexmark</code-name-base>
<build-prerequisite/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@

import java.awt.Image;
import java.beans.PropertyVetoException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;
import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataLoaderPool;
Expand All @@ -41,7 +39,7 @@
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;

/**
*
Expand All @@ -57,7 +55,7 @@ public class GenericDataObject extends MultiDataObject {
public GenericDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException, IOException {
super(pf, loader);
this.mimeType = FileUtil.getMIMEType(pf);
registerEditor(mimeType, false);
registerEditor(mimeType, true);
synchronized (REGISTRY) {
REGISTRY.add(new WeakReference<>(this));
}
Expand Down Expand Up @@ -144,5 +142,8 @@ public static Factory factory() {
}
};
}
}

public static MultiViewEditorElement createEditor(Lookup lkp) {
return new MultiViewEditorElement(lkp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
import javax.swing.event.ChangeEvent;
import org.eclipse.tm4e.core.internal.grammar.raw.RawGrammarReader;
import org.eclipse.tm4e.core.registry.IGrammarSource;
import org.netbeans.core.spi.multiview.MultiViewFactory;
import org.netbeans.modules.textmate.lexer.TextmateTokenId;
import org.netbeans.spi.navigator.NavigatorPanel;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataLoaderPool;
import org.openide.loaders.DataObject;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.openide.util.NbPreferences;

/**
Expand All @@ -57,6 +59,7 @@ static List<LanguageDescription> load() {
return Arrays.stream(new Gson().fromJson(descriptions, LanguageDescription[].class)).collect(Collectors.toList());
}

@Messages("Source=&Source")
static void store(List<LanguageDescription> languages) {
Set<String> originalMimeTypes = load().stream().map(ld -> ld.mimeType).collect(Collectors.toSet());
Set<String> mimeTypesToClear = new HashSet<>(originalMimeTypes);
Expand All @@ -71,7 +74,7 @@ static void store(List<LanguageDescription> languages) {
Exceptions.printStackTrace(ex);
}
}

for (FileObject children : mimeResolver.getChildren()) {
if ("synthetic".equals(children.getAttribute(LanguageServersPanel.class.getName()))) {
try {
Expand Down Expand Up @@ -130,6 +133,22 @@ static void store(List<LanguageDescription> languages) {
loader.setAttribute("dataObjectClass", GenericDataObject.class.getName());
loader.setAttribute("mimeType", description.mimeType);

FileObject multiViewRegistration = FileUtil.getConfigFile("Editors/" + description.mimeType + "/MultiView/source.instance");
if (multiViewRegistration != null) {
multiViewRegistration.delete();
}
multiViewRegistration = FileUtil.createData(FileUtil.getConfigRoot(), "Editors/" + description.mimeType + "/MultiView/source.instance");
Method createMultiViewDescription = MultiViewFactory.class.getDeclaredMethod("createMultiViewDescription", Map.class);
multiViewRegistration.setAttribute("methodvalue:instanceCreate", createMultiViewDescription);
multiViewRegistration.setAttribute("instanceClass", "org.netbeans.core.multiview.ContextAwareDescription");
multiViewRegistration.setAttribute("class", GenericDataObject.class.getName());
multiViewRegistration.setAttribute("mimeType", description.mimeType);
multiViewRegistration.setAttribute("displayName", Bundle.Source());
multiViewRegistration.setAttribute("preferredID", "lsp.source");
multiViewRegistration.setAttribute("persistenceType", 1);
multiViewRegistration.setAttribute("position", 100);
multiViewRegistration.setAttribute("method", "createEditor");

FileObject icon = FileUtil.getConfigFile("Loaders/" + description.mimeType + "/Factories/icon.png");
if (icon != null) {
icon.delete();
Expand All @@ -143,16 +162,17 @@ static void store(List<LanguageDescription> languages) {
}

loader.setAttribute("iconBase", icon.getNameExt());
multiViewRegistration.setAttribute("iconBase", icon.getNameExt());
}

if (description.languageServer != null && !description.languageServer.isEmpty()) {
FileObject langServer = FileUtil.createData(FileUtil.getConfigRoot(), "Editors/" + description.mimeType + "/org-netbeans-modules-lsp-client-options-GenericLanguageServer.instance");
langServer.setAttribute("command", description.languageServer.split(" "));
if (description.name != null) {
langServer.setAttribute("name", description.name);
}
}

mimeTypesToClear.remove(description.mimeType);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
Expand Down
Loading