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

XML custom client commands #942

Closed
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
4 changes: 3 additions & 1 deletion org.eclipse.wildwebdeveloper.xml/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Require-Bundle: org.eclipse.tm4e.registry;bundle-version="0.3.0",
org.eclipse.core.net;bundle-version="1.3.0",
org.eclipse.lsp4j.jsonrpc,
org.eclipse.text,
org.eclipse.jface.text;bundle-version="3.20.100"
org.eclipse.jface.text;bundle-version="3.20.100",
org.eclipse.ui.workbench.texteditor;bundle-version="3.16.600",
com.google.gson
Bundle-ActivationPolicy: lazy
Bundle-Activator: org.eclipse.wildwebdeveloper.xml.internal.Activator
Export-Package: org.eclipse.wildwebdeveloper.xml;x-friends:="org.eclipse.m2e.editor.lemminx"
17 changes: 17 additions & 0 deletions org.eclipse.wildwebdeveloper.xml/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,21 @@
class="org.eclipse.wildwebdeveloper.xml.internal.ui.preferences.XMLPreferenceInitializer">
</initializer>
</extension>

<extension
point="org.eclipse.ui.handlers">
<handler
class="org.eclipse.wildwebdeveloper.xml.internal.commands.OpenUriHandler"
commandId="xml.open.uri">
</handler>
<handler
class="org.eclipse.wildwebdeveloper.xml.internal.commands.FindReferencesHandler"
commandId="xml.show.references">
</handler>
<handler
class="org.eclipse.wildwebdeveloper.xml.internal.commands.GrammarAssociationHandler"
commandId="xml.open.binding.wizard">
</handler>
</extension>

</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -160,7 +161,25 @@ public String toString() {

@Override
public Object getInitializationOptions(URI rootUri) {
return mergeCustomInitializationOptions(extensionJarRegistry.getInitiatizationOptions());
Map<String, Object> initializationOptions = new HashMap<>();
Map<String, Object> settings = mergeCustomInitializationOptions(
extensionJarRegistry.getInitiatizationOptions());
initializationOptions.put(SETTINGS_KEY, settings.get(SETTINGS_KEY));
Object extendedClientCapabilities = createExtendedClientCapabilities();
initializationOptions.put("extendedClientCapabilities", extendedClientCapabilities);
return initializationOptions;
}

private Object createExtendedClientCapabilities() {
Map<String, Object> extendedClientCapabilities = new HashMap<>();
Map<String, Object> codeLens = new HashMap<>();
extendedClientCapabilities.put("codeLens", codeLens);
Map<String, Object> codeLensKind = new HashMap<>();
codeLens.put("codeLensKind", codeLensKind);
List<String> valueSet = Arrays.asList("references", "open.uri", "association");
codeLensKind.put("valueSet", valueSet);
extendedClientCapabilities.put("bindingWizardSupport", Boolean.TRUE);
return extendedClientCapabilities;
}

private static Map<String, Object> mergeCustomInitializationOptions(Map<String, Object> defaults) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package org.eclipse.wildwebdeveloper.xml.internal.commands;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog;

public class AssociateGrammarDialog extends TitleAreaDialog {

private final List<String> W3C_GRAMMAR_FILE_EXTENSION = Arrays.asList("xsd", "dtd");

private final List<String> RELAXNG_GRAMMAR_FILE_EXTENSION = Arrays.asList("rng", "rnc");

private final List<String> ALL_GRAMMAR_FILE_EXTENSION = Stream
.concat(W3C_GRAMMAR_FILE_EXTENSION.stream(), RELAXNG_GRAMMAR_FILE_EXTENSION.stream()).toList();

private final static String MESSAGE4 = "The grammar file is required.";

private final static String MESSAGE = "The grammar file ''{0}'' doesn''t exists.";

private final static String MESSAGE2 = "The grammar file ''{0}'' should have ''{1}'' file extension.";

private final static String MESSAGE3 = "The RelaxNG grammar file ''{0}'' cannot be associated with ''{1}'' type.";

public static enum BindingType {

standard("standard", "Standard (xsi, DOCTYPE)"), xmlmodel("xml-model", "XML Model association");

private final String code;
private final String label;

private BindingType(String code, String label) {
this.code = code;
this.label = label;
}

public String getCode() {
return code;
}

public String getLabel() {
return label;
}
}

private final IPath filePath;
private final IContainer resourceContainer;

private Text grammarURIText;
private ComboViewer bindingTypeViewer;
private String grammarURI;
private BindingType bindingType;

public AssociateGrammarDialog(Shell parentShell, IPath filePath, IContainer resourceContainer) {
super(parentShell);
this.filePath = filePath;
this.resourceContainer = resourceContainer;
}

@Override
public void create() {
super.create();
setTitle("Associate grammar");
setMessage("Fill the XSD, DTD, RelaxNG grammar URI", IMessageProvider.INFORMATION);
validate();
}

@Override
protected Control createDialogArea(Composite parent) {
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
GridLayout layout = new GridLayout(3, false);
container.setLayout(layout);

createFirstName(container);
createLastName(container);
return area;
}

private void createFirstName(Composite container) {
Label lbtFirstName = new Label(container, SWT.NONE);
lbtFirstName.setText("Grammar URI");

GridData dataFirstName = new GridData();
dataFirstName.grabExcessHorizontalSpace = true;
dataFirstName.horizontalAlignment = GridData.FILL;

grammarURIText = new Text(container, SWT.BORDER);
grammarURIText.addModifyListener(e -> validate());
grammarURIText.setLayoutData(dataFirstName);

Button browseButton = new Button(container, SWT.NONE);
browseButton.setText("Browse...");
browseButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FilteredResourcesSelectionDialog dialog = new FilteredResourcesSelectionDialog(getParentShell(), false,
resourceContainer, IResource.FILE);
dialog.setInitialPattern("*.");
if (dialog.open() == OK) {
IFile selectedFile = (IFile) dialog.getFirstResult();
IPath absolutePath = selectedFile.getFullPath().makeRelativeTo(filePath.removeLastSegments(1));
grammarURIText.setText(absolutePath.toString());
}
}
});
}

private void createLastName(Composite container) {
Label lbtLastName = new Label(container, SWT.NONE);
lbtLastName.setText("Association type:");

GridData dataLastName = new GridData();
dataLastName.grabExcessHorizontalSpace = true;
dataLastName.horizontalAlignment = GridData.FILL;
dataLastName.horizontalSpan = 2;
bindingTypeViewer = new ComboViewer(container);
bindingTypeViewer.setContentProvider(ArrayContentProvider.getInstance());
bindingTypeViewer.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
if (element instanceof BindingType) {
BindingType bindingType = (BindingType) element;
return bindingType.getLabel();
}
return super.getText(element);
}
});
bindingTypeViewer.getCombo().setLayoutData(dataLastName);
bindingTypeViewer.setInput(BindingType.values());
bindingTypeViewer.setSelection(new StructuredSelection(BindingType.standard));
}

@Override
protected boolean isResizable() {
return true;
}

// save content of the Text fields because they get disposed
// as soon as the Dialog closes
private void saveInput() {
grammarURI = grammarURIText.getText();
bindingType = (BindingType) bindingTypeViewer.getStructuredSelection().getFirstElement();

}

private void validate() {
try {
String grammarURI = grammarURIText.getText();
if (grammarURI.isBlank()) {
setErrorMessage(MESSAGE4);
return;
}
IFile file = resourceContainer.getFile(new Path(grammarURI));
if (file == null || !file.exists()) {
setMessage(MessageFormat.format(MESSAGE, grammarURI), IMessageProvider.WARNING);
return;
} else if (isRelaxNGGrammarFile(file.getFileExtension())) {
BindingType bindingType = (BindingType) bindingTypeViewer.getStructuredSelection().getFirstElement();
if (bindingType == BindingType.standard) {
setErrorMessage(MessageFormat.format(MESSAGE3, grammarURI, BindingType.standard.getLabel()));
return;
}
} else if (!isStandardGramarFile(file.getFileExtension())) {
setMessage(
MessageFormat.format(MESSAGE2, grammarURI,
ALL_GRAMMAR_FILE_EXTENSION.stream().collect(Collectors.joining(","))),
IMessageProvider.WARNING);
return;
}
setMessage("OK", IMessageProvider.WARNING);
} finally {
super.getButton(IDialogConstants.OK_ID).setEnabled(getErrorMessage() == null);
}
}

private boolean isStandardGramarFile(String fileExtension) {
return W3C_GRAMMAR_FILE_EXTENSION.contains(fileExtension) || isRelaxNGGrammarFile(fileExtension);
}

private boolean isRelaxNGGrammarFile(String fileExtension) {
return RELAXNG_GRAMMAR_FILE_EXTENSION.contains(fileExtension);
}

@Override
protected void okPressed() {
saveInput();
super.okPressed();
}

public String getGrammarURI() {
return grammarURI;
}

public BindingType getBindingType() {
return bindingType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.eclipse.wildwebdeveloper.xml.internal.commands;

import java.util.Map;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.command.LSPCommandHandler;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Position;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.wildwebdeveloper.xml.internal.Activator;

public class FindReferencesHandler extends LSPCommandHandler {

@Override
public Object execute(ExecutionEvent event, Command command, IPath path) throws ExecutionException {
IEditorPart part = HandlerUtil.getActiveEditor(event);
if (part instanceof ITextEditor editor) {
IDocument document = LSPEclipseUtils.getDocument(editor);
if (document == null) {
return null;
}
try {
Map<String, Object> position = (Map<String, Object>) command.getArguments().get(1);
int line = ((Number) position.get("line")).intValue();
int character = ((Number) position.get("character")).intValue();
int offset = LSPEclipseUtils.toOffset(new Position(line, character), document);
LSPEclipseUtils.searchLSPReferences(document, offset, HandlerUtil.getActiveShell(event).getDisplay());
} catch (BadLocationException e) {
Activator.getDefault().getLog().error("Error while getting offset for references", e);
}
}
return null;
}
}
Loading