Toggle comment implementation for application.properties and .yml
This commit is contained in:
@@ -110,9 +110,27 @@
|
||||
class="org.springframework.tooling.boot.ls.commands.OpenResourceInEditor"
|
||||
commandId="org.springframework.tooling.boot.ls.OpenResourceInEditor">
|
||||
</handler>
|
||||
<handler
|
||||
class="org.springframework.tooling.boot.ls.commands.ToggleComment"
|
||||
commandId="org.springframework.tooling.boot.ls.ToggleComment">
|
||||
<activeWhen>
|
||||
<with variable="activeEditorId">
|
||||
<or>
|
||||
<equals value="SpringBootPropertyEditor"/>
|
||||
<equals value="SpringBootYMLPropertyEditor"/>
|
||||
</or>
|
||||
</with>
|
||||
</activeWhen>
|
||||
</handler>
|
||||
</extension>
|
||||
<extension
|
||||
point="org.eclipse.ui.commands">
|
||||
<!-- !!! category for command is required or keybindings don't work !!! -->
|
||||
<category
|
||||
description="Spring Language Server Commands"
|
||||
id="org.springframework.ide.eclipse.commands"
|
||||
name="Spring Generic Text Editor">
|
||||
</category>
|
||||
<command
|
||||
id="org.springframework.tooling.boot.ls.OpenJavaType"
|
||||
name="Open Java Type in Editor">
|
||||
@@ -131,9 +149,52 @@
|
||||
optional="false">
|
||||
</commandParameter>
|
||||
</command>
|
||||
<command
|
||||
id="org.springframework.tooling.boot.ls.ToggleComment"
|
||||
categoryId="org.springframework.ide.eclipse.commands"
|
||||
name="Toggle Comment">
|
||||
</command>
|
||||
</extension>
|
||||
|
||||
|
||||
|
||||
<extension
|
||||
point="org.eclipse.ui.bindings">
|
||||
<!-- win32: M1=CTRL, M2=SHIFT, M3=ALT, M4=-
|
||||
carbon: M1=COMMAND, M2=SHIFT, M3=ALT, M4=CTRL -->
|
||||
<key
|
||||
sequence="M1+M2+C"
|
||||
contextId="org.eclipse.ui.textEditorScope"
|
||||
commandId="org.springframework.tooling.boot.ls.ToggleComment"
|
||||
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
|
||||
<key
|
||||
sequence="M1+/"
|
||||
contextId="org.eclipse.ui.textEditorScope"
|
||||
commandId="org.springframework.tooling.boot.ls.ToggleComment"
|
||||
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
|
||||
</extension>
|
||||
|
||||
<!-- Note: locationURI below adds this to 'edit' menu. Not a great place. Decided instead to just
|
||||
disable this. If we want to show this in a menu somewhere we should decide where and figure out how/if we
|
||||
can make it apear there. -->
|
||||
<!-- <extension
|
||||
point="org.eclipse.ui.menus">
|
||||
<menuContribution
|
||||
locationURI="menu:edit?after=selectAll">
|
||||
<command
|
||||
commandId="org.springframework.tooling.boot.ls.ToggleComment"
|
||||
label="Toggle Comment Spring"
|
||||
style="push">
|
||||
<visibleWhen
|
||||
checkEnabled="false">
|
||||
<with
|
||||
variable="activePartId">
|
||||
<equals
|
||||
value="SpringBootPropertyEditor">
|
||||
</equals>
|
||||
</with>
|
||||
</visibleWhen>
|
||||
</command>
|
||||
</menuContribution>
|
||||
</extension> -->
|
||||
<extension
|
||||
point="org.eclipse.ui.editors">
|
||||
<editor
|
||||
@@ -149,16 +210,6 @@
|
||||
</editor>
|
||||
</extension>
|
||||
|
||||
<!--
|
||||
<extension
|
||||
point="org.eclipse.ui.editors">
|
||||
<editorContentTypeBinding
|
||||
contentTypeId="org.springframework.boot.ide.properties.application.properties"
|
||||
editorId="org.eclipse.ui.genericeditor.GenericEditor">
|
||||
</editorContentTypeBinding>
|
||||
</extension>
|
||||
-->
|
||||
|
||||
<extension
|
||||
point="org.eclipse.ui.editors">
|
||||
<editor
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2018 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.tooling.boot.ls.commands;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.eclipse.core.commands.AbstractHandler;
|
||||
import org.eclipse.core.commands.ExecutionEvent;
|
||||
import org.eclipse.core.commands.ExecutionException;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.jface.text.TextSelection;
|
||||
import org.eclipse.jface.viewers.ISelection;
|
||||
import org.eclipse.text.edits.DeleteEdit;
|
||||
import org.eclipse.text.edits.InsertEdit;
|
||||
import org.eclipse.text.edits.MalformedTreeException;
|
||||
import org.eclipse.text.edits.MultiTextEdit;
|
||||
import org.eclipse.text.undo.DocumentUndoManagerRegistry;
|
||||
import org.eclipse.text.undo.IDocumentUndoManager;
|
||||
import org.eclipse.ui.IEditorInput;
|
||||
import org.eclipse.ui.IEditorPart;
|
||||
import org.eclipse.ui.editors.text.TextEditor;
|
||||
import org.eclipse.ui.handlers.HandlerUtil;
|
||||
import org.springsource.ide.eclipse.commons.livexp.util.Log;
|
||||
|
||||
public class ToggleComment extends AbstractHandler {
|
||||
|
||||
Pattern commmentPattern = Pattern.compile("^\\s*#");
|
||||
String commentPrefix = "#";
|
||||
|
||||
@Override
|
||||
public Object execute(ExecutionEvent event) throws ExecutionException {
|
||||
Object ei = HandlerUtil.getVariable(event, "activeEditorId");
|
||||
System.out.println(ei);
|
||||
try {
|
||||
ISelection sel = HandlerUtil.getCurrentSelection(event);
|
||||
if (sel instanceof TextSelection) {
|
||||
TextSelection textSel = (TextSelection) sel;
|
||||
int startLine = textSel.getStartLine();
|
||||
int endLine = textSel.getEndLine();
|
||||
|
||||
IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event);
|
||||
System.out.println(editorInput);
|
||||
IEditorPart editor = HandlerUtil.getActiveEditor(event);
|
||||
if (editor instanceof TextEditor) {
|
||||
TextEditor textEditor = (TextEditor) editor;
|
||||
IDocument doc = textEditor.getDocumentProvider().getDocument(editorInput);
|
||||
if (doc!=null) {
|
||||
System.out.println(doc.get());
|
||||
|
||||
boolean hasComments = hasComments(doc, startLine, endLine);
|
||||
IDocumentUndoManager undo = DocumentUndoManagerRegistry.getDocumentUndoManager(doc);
|
||||
undo.beginCompoundChange();
|
||||
try {
|
||||
if (hasComments) {
|
||||
stripComments(doc, startLine, endLine);
|
||||
} else {
|
||||
addComments(doc, startLine, endLine);
|
||||
}
|
||||
} finally {
|
||||
undo.endCompoundChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.log(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addComments(IDocument doc, int startLine, int endLine) throws MalformedTreeException, BadLocationException {
|
||||
MultiTextEdit edits = new MultiTextEdit();
|
||||
for (int line = startLine; line<=endLine; line++) {
|
||||
int lineStart = doc.getLineOffset(line);
|
||||
edits.addChild(new InsertEdit(lineStart, "#"));
|
||||
}
|
||||
System.out.println("--- appling edits ---");
|
||||
edits.apply(doc);
|
||||
System.out.println(doc.get());
|
||||
}
|
||||
|
||||
private void stripComments(IDocument doc, int startLine, int endLine) throws MalformedTreeException, BadLocationException {
|
||||
MultiTextEdit edits = new MultiTextEdit();
|
||||
for (int line = startLine; line<=endLine; line++) {
|
||||
int lineStart = doc.getLineOffset(line);
|
||||
String lineText = getLineText(doc, line);
|
||||
Matcher matcher = commmentPattern.matcher(lineText);
|
||||
if (matcher.find()) {
|
||||
int end = lineStart + matcher.end();
|
||||
int start = end - commentPrefix.length();
|
||||
edits.addChild(new DeleteEdit(start, commentPrefix.length()));
|
||||
}
|
||||
}
|
||||
System.out.println("--- appling edits ---");
|
||||
edits.apply(doc);
|
||||
System.out.println(doc.get());
|
||||
}
|
||||
|
||||
private boolean hasComments(IDocument doc, int startLine, int endLine) throws BadLocationException {
|
||||
for (int line = startLine; line <= endLine; line++) {
|
||||
String lineText = getLineText(doc, line);
|
||||
if (!commmentPattern.matcher(lineText).find()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private String getLineText(IDocument doc, int line) throws BadLocationException {
|
||||
return doc.get(doc.getLineOffset(line), doc.getLineLength(line));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,11 +24,9 @@ import org.eclipse.ui.texteditor.ChainedPreferenceStore;
|
||||
* Has spaces rather than tab for indentation
|
||||
*
|
||||
* @author Alex Boyko
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("restriction")
|
||||
public class YamlGenericEditor extends ExtensionBasedTextEditor {
|
||||
|
||||
@Override
|
||||
protected boolean isTabsToSpacesConversionEnabled() {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user