Skip to content

Commit

Permalink
Added menu to refactor commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Mar 29, 2024
1 parent 5e66b31 commit f6b74f9
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 60 deletions.
127 changes: 126 additions & 1 deletion plugins/org.eclipse.acceleo.aql.ide.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
class="org.eclipse.acceleo.aql.ide.ui.property.PropertyTester"
id="org.eclipse.acceleo.aql.ide.ui.property.propertyTester"
namespace="org.eclipse.acceleo.aql.ide.ui.property"
properties="isMain"
properties="isMain,isBlockSelection"
type="java.lang.Object">
</propertyTester>
</extension>
Expand Down Expand Up @@ -191,22 +191,67 @@
<handler
class="org.eclipse.acceleo.aql.ide.ui.handlers.ExtractTemplateHandler"
commandId="org.eclipse.acceleo.aql.ide.ui.commands.extractTemplate">
<enabledWhen>
<with
variable="selection">
<test
forcePluginActivation="true"
property="org.eclipse.acceleo.aql.ide.ui.property.isBlockSelection">
</test>
</with>
</enabledWhen>
</handler>
<handler
class="org.eclipse.acceleo.aql.ide.ui.handlers.WrapInForHandler"
commandId="org.eclipse.acceleo.aql.ide.ui.commands.wrapInFor">
<enabledWhen>
<with
variable="selection">
<test
forcePluginActivation="true"
property="org.eclipse.acceleo.aql.ide.ui.property.isBlockSelection">
</test>
</with>
</enabledWhen>
</handler>
<handler
class="org.eclipse.acceleo.aql.ide.ui.handlers.WrapInIfHandler"
commandId="org.eclipse.acceleo.aql.ide.ui.commands.wrapInIf">
<enabledWhen>
<with
variable="selection">
<test
forcePluginActivation="true"
property="org.eclipse.acceleo.aql.ide.ui.property.isBlockSelection">
</test>
</with>
</enabledWhen>
</handler>
<handler
class="org.eclipse.acceleo.aql.ide.ui.handlers.WrapInLetHandler"
commandId="org.eclipse.acceleo.aql.ide.ui.commands.wrapInLet">
<enabledWhen>
<with
variable="selection">
<test
forcePluginActivation="true"
property="org.eclipse.acceleo.aql.ide.ui.property.isBlockSelection">
</test>
</with>
</enabledWhen>
</handler>
<handler
class="org.eclipse.acceleo.aql.ide.ui.handlers.WrapInProtectedHandler"
commandId="org.eclipse.acceleo.aql.ide.ui.commands.wrapInProtected">
<enabledWhen>
<with
variable="selection">
<test
forcePluginActivation="true"
property="org.eclipse.acceleo.aql.ide.ui.property.isBlockSelection">
</test>
</with>
</enabledWhen>
</handler>
</extension>
<extension
Expand All @@ -227,6 +272,86 @@
icon="icons/AcceleoEditor.gif"
id="org.eclipse.acceleo.aql.ide.ui.menus.createEclipseUIGeneratorProjectCommand">
</command>
<command
commandId="org.eclipse.acceleo.aql.ide.ui.commands.extractTemplate"
icon="icons/AcceleoEditor.gif"
id="org.eclipse.acceleo.aql.ide.ui.menus.extractTemplate"
style="push"
tooltip="Extract the selection to a template">
<visibleWhen
checkEnabled="false">
<with
variable="selection">
<instanceof
value="org.eclipse.jface.text.TextSelection">
</instanceof>
</with>
</visibleWhen>
</command>
<command
commandId="org.eclipse.acceleo.aql.ide.ui.commands.wrapInFor"
icon="icons/AcceleoEditor.gif"
id="org.eclipse.acceleo.aql.ide.ui.menus.wrapInFor"
style="push"
tooltip="Wrap the selection in a for statement">
<visibleWhen
checkEnabled="false">
<with
variable="selection">
<instanceof
value="org.eclipse.jface.text.TextSelection">
</instanceof>
</with>
</visibleWhen>
</command>
<command
commandId="org.eclipse.acceleo.aql.ide.ui.commands.wrapInIf"
icon="icons/AcceleoEditor.gif"
id="org.eclipse.acceleo.aql.ide.ui.menus.wrapInIf"
style="push"
tooltip="Wrap the selection in a if statement">
<visibleWhen
checkEnabled="false">
<with
variable="selection">
<instanceof
value="org.eclipse.jface.text.TextSelection">
</instanceof>
</with>
</visibleWhen>
</command>
<command
commandId="org.eclipse.acceleo.aql.ide.ui.commands.wrapInLet"
icon="icons/AcceleoEditor.gif"
id="org.eclipse.acceleo.aql.ide.ui.menus.wrapInLet"
style="push"
tooltip="Wrap the selection in a let statement">
<visibleWhen
checkEnabled="false">
<with
variable="selection">
<instanceof
value="org.eclipse.jface.text.TextSelection">
</instanceof>
</with>
</visibleWhen>
</command>
<command
commandId="org.eclipse.acceleo.aql.ide.ui.commands.wrapInProtected"
icon="icons/AcceleoEditor.gif"
id="org.eclipse.acceleo.aql.ide.ui.menus.wrapInProtected"
style="push"
tooltip="Wrap the selection in a protected statement">
<visibleWhen
checkEnabled="false">
<with
variable="selection">
<instanceof
value="org.eclipse.jface.text.TextSelection">
</instanceof>
</with>
</visibleWhen>
</command>
</menu>
</menuContribution>
</extension>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

import org.eclipse.acceleo.aql.ide.AcceleoPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.TextEditor;

public class PropertyTester extends org.eclipse.core.expressions.PropertyTester {

Expand All @@ -28,6 +34,32 @@ public boolean test(Object receiver, String property, Object[] args, Object expe
}
break;

case "isBlockSelection":
if (receiver instanceof ITextSelection) {
final ITextSelection selection = (ITextSelection)receiver;
final IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
if (activeWorkbenchWindow != null) {
final IEditorPart activeEditor = activeWorkbenchWindow.getActivePage()
.getActiveEditor();
if (activeEditor instanceof TextEditor) {
final IDocument document = ((TextEditor)activeEditor).getDocumentProvider()
.getDocument(activeEditor.getEditorInput());
final boolean startAtNewLine = selection.getOffset() == 0 || document.get()
.charAt(selection.getOffset() - 1) == '\n';
res = startAtNewLine && !selection.isEmpty() && selection.getText().endsWith(
"\n");
} else {
res = false;
}
} else {
res = false;
}
} else {
res = false;
}
break;

default:
throw new IllegalStateException("Unkown property" + property);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.acceleo.IfStatement;
import org.eclipse.acceleo.LetStatement;
import org.eclipse.acceleo.ProtectedArea;
import org.eclipse.acceleo.Query;
import org.eclipse.acceleo.Template;
import org.eclipse.acceleo.aql.ls.services.textdocument.AcceleoTextDocument;
import org.eclipse.acceleo.aql.ls.services.textdocument.AcceleoTextDocumentService;
Expand Down Expand Up @@ -68,11 +67,6 @@ public class AcceleoLanguageServer implements LanguageServer, LanguageClientAwar
*/
public static final String WRAP_IN_IF_COMMAND = "wrapInIf";

/**
* Extract {@link Query} command.
*/
public static final String EXTRACT_QUERY_COMMAND = "extractQuery";

/**
* Extract {@link Template} command.
*/
Expand Down Expand Up @@ -188,7 +182,6 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {

final List<String> commands = new ArrayList<>();
commands.add(EXTRACT_TEMPLATE_COMMAND);
commands.add(EXTRACT_QUERY_COMMAND);
commands.add(WRAP_IN_IF_COMMAND);
commands.add(WRAP_IN_FOR_COMMAND);
commands.add(WRAP_IN_LET_COMMAND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.eclipse.acceleo.aql.ls.common.AcceleoLanguageServerServicesUtils;
import org.eclipse.acceleo.aql.ls.services.textdocument.AcceleoTextDocument;
import org.eclipse.acceleo.aql.ls.services.workspace.command.DocumentRangeParams;
import org.eclipse.acceleo.aql.ls.services.workspace.command.ExtractQueryCommand;
import org.eclipse.acceleo.aql.ls.services.workspace.command.ExtractTemplateCommand;
import org.eclipse.acceleo.aql.ls.services.workspace.command.WrapInForCommand;
import org.eclipse.acceleo.aql.ls.services.workspace.command.WrapInIfCommand;
Expand Down Expand Up @@ -116,8 +115,6 @@ public void didChangeWorkspaceFolders(DidChangeWorkspaceFoldersParams params) {

@Override
public CompletableFuture<Object> executeCommand(ExecuteCommandParams params) {
final CompletableFuture<Object> res;

final DocumentRangeParams documentRangeParams = new Gson().fromJson((JsonObject)params.getArguments()
.get(0), DocumentRangeParams.class);
final Range range = documentRangeParams.getRange();
Expand All @@ -128,14 +125,6 @@ public CompletableFuture<Object> executeCommand(ExecuteCommandParams params) {
final CompletableFuture<Object> workspaceEdit;
final String label;
switch (params.getCommand()) {
case AcceleoLanguageServer.EXTRACT_QUERY_COMMAND:
workspaceEdit = CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
return new ExtractQueryCommand().exec(document, range);
});
label = "Extract Query";
break;

case AcceleoLanguageServer.EXTRACT_TEMPLATE_COMMAND:
workspaceEdit = CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {
.getStart(), document.getContents());
final int endIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(range
.getEnd(), document.getContents());
if (isSameBlock(document, startIndex, endIndex)) {
final String text = document.getContents().substring(startIndex, endIndex);
if (!text.isEmpty() && isSameBlock(document, startIndex, endIndex)) {
final Map<String, List<TextEdit>> changes = new LinkedHashMap<>();

final Template template = AcceleoPackage.eINSTANCE.getAcceleoFactory().createTemplate();
Expand All @@ -56,7 +57,6 @@ public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {
// TODO pass the new line String
// create the template
final String lineDelimiter = System.lineSeparator();
final String text = document.getContents().substring(startIndex, endIndex);
final String blockIndentation = getBlockIndentation(text);
template.setBody(createBlock(text, blockIndentation, " ", lineDelimiter, true));
final List<Variable> parameters = getVariablesDeclaredOutSide(document, startIndex, endIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {
.getStart(), document.getContents());
final int endIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(range
.getEnd(), document.getContents());
if (isSameBlock(document, startIndex, endIndex)) {
final String text = document.getContents().substring(startIndex, endIndex);
if (!text.isEmpty() && isSameBlock(document, startIndex, endIndex)) {
final Map<String, List<TextEdit>> changes = new LinkedHashMap<>();

final ForStatement statement = AcceleoPackage.eINSTANCE.getAcceleoFactory()
Expand All @@ -61,7 +62,6 @@ public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {

// TODO pass the new line String
final String lineDelimiter = System.lineSeparator();
final String text = document.getContents().substring(startIndex, endIndex);
final String blockIndentation = getBlockIndentation(text);
statement.setBody(createBlock(text, blockIndentation, " ", lineDelimiter, false));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {
.getStart(), document.getContents());
final int endIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(range
.getEnd(), document.getContents());
if (isSameBlock(document, startIndex, endIndex)) {
final String text = document.getContents().substring(startIndex, endIndex);
if (!text.isEmpty() && isSameBlock(document, startIndex, endIndex)) {
final Map<String, List<TextEdit>> changes = new LinkedHashMap<>();

final IfStatement statement = AcceleoPackage.eINSTANCE.getAcceleoFactory()
Expand All @@ -57,7 +58,6 @@ public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {

// TODO pass the new line String
final String lineDelimiter = System.lineSeparator();
final String text = document.getContents().substring(startIndex, endIndex);
final String blockIndentation = getBlockIndentation(text);
statement.setThen(createBlock(text, blockIndentation, " ", lineDelimiter, false));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {
.getStart(), document.getContents());
final int endIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(range
.getEnd(), document.getContents());
if (isSameBlock(document, startIndex, endIndex)) {
final String text = document.getContents().substring(startIndex, endIndex);
if (!text.isEmpty() && isSameBlock(document, startIndex, endIndex)) {
final Map<String, List<TextEdit>> changes = new LinkedHashMap<>();

final LetStatement statement = AcceleoPackage.eINSTANCE.getAcceleoFactory()
Expand All @@ -61,7 +62,6 @@ public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {

// TODO pass the new line String
final String lineDelimiter = System.lineSeparator();
final String text = document.getContents().substring(startIndex, endIndex);
final String blockIndentation = getBlockIndentation(text);
statement.setBody(createBlock(text, blockIndentation, " ", lineDelimiter, false));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {
.getStart(), document.getContents());
final int endIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(range
.getEnd(), document.getContents());
if (isSameBlock(document, startIndex, endIndex)) {
final String text = document.getContents().substring(startIndex, endIndex);
if (!text.isEmpty() && isSameBlock(document, startIndex, endIndex)) {
final Map<String, List<TextEdit>> changes = new LinkedHashMap<>();

final ProtectedArea statement = AcceleoPackage.eINSTANCE.getAcceleoFactory()
Expand All @@ -57,7 +58,6 @@ public WorkspaceEdit exec(AcceleoTextDocument document, Range range) {

// TODO pass the new line String
final String lineDelimiter = System.lineSeparator();
final String text = document.getContents().substring(startIndex, endIndex);
final String blockIndentation = getBlockIndentation(text);
statement.setBody(createBlock(text, blockIndentation, " ", lineDelimiter, false));

Expand Down

0 comments on commit f6b74f9

Please sign in to comment.