Search in sources :

Example 1 with DocumentCommand

use of org.eclipse.jface.text.DocumentCommand in project xtext-eclipse by eclipse.

the class MultiLineTerminalsEditStrategyTest method testFindStopTerminal_3.

/**
 * See bug 403812
 * If all partitions after the inner closing brace are of type __sl_comment there should be
 * a new closing brace as well.
 */
@Test
public void testFindStopTerminal_3() throws Exception {
    MultiLineTerminalsEditStrategy strategy = new MultiLineTerminalsEditStrategy("{", "\t", "}", true);
    DocumentCommand command = DocumentCommandTest.getDocumentCommandTest(17);
    strategy.customizeDocumentCommand(getDocument("Hello {\n  Thomas{\n   Birthday\n }//}!"), command);
    assertEquals("}", command.text.trim());
}
Also used : MultiLineTerminalsEditStrategy(org.eclipse.xtext.ui.editor.autoedit.MultiLineTerminalsEditStrategy) DocumentCommand(org.eclipse.jface.text.DocumentCommand) AbstractXtextDocumentTest(org.eclipse.xtext.ui.tests.editor.model.AbstractXtextDocumentTest) Test(org.junit.Test)

Example 2 with DocumentCommand

use of org.eclipse.jface.text.DocumentCommand in project xtext-eclipse by eclipse.

the class MultiLineTerminalsEditStrategyTest method testFindStopTerminal.

/**
 * 	See bug 403812
 * 	Closing terminal is already included, so that the command must not contain a new closing brace
 */
@Test
public void testFindStopTerminal() throws Exception {
    MultiLineTerminalsEditStrategy strategy = new MultiLineTerminalsEditStrategy("{", "\t", "}", true);
    DocumentCommand command = DocumentCommandTest.getDocumentCommandTest(17);
    strategy.customizeDocumentCommand(getDocument("Hello {\n  Thomas{\n   Birthday\n  }//\n}!"), command);
    assertEquals("", command.text.trim());
}
Also used : MultiLineTerminalsEditStrategy(org.eclipse.xtext.ui.editor.autoedit.MultiLineTerminalsEditStrategy) DocumentCommand(org.eclipse.jface.text.DocumentCommand) AbstractXtextDocumentTest(org.eclipse.xtext.ui.tests.editor.model.AbstractXtextDocumentTest) Test(org.junit.Test)

Example 3 with DocumentCommand

use of org.eclipse.jface.text.DocumentCommand in project tm4e by eclipse.

the class LanguageConfigurationAutoEditStrategy method customizeDocumentCommand.

@Override
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
    IContentType[] contentTypes = findContentTypes(document);
    if (contentTypes == null || command.text.isEmpty()) {
        return;
    }
    installViewer();
    if (TextUtils.isEnter(document, command)) {
        // key enter pressed
        onEnter(document, command);
        return;
    }
    // Auto close pair
    LanguageConfigurationRegistryManager registry = LanguageConfigurationRegistryManager.getInstance();
    for (IContentType contentType : contentTypes) {
        CharacterPair autoClosingPair = registry.getAutoClosePair(document.get(), command.offset, command.text, contentType);
        if (autoClosingPair == null) {
            continue;
        }
        command.caretOffset = command.offset + command.text.length();
        command.shiftsCaret = false;
        if (command.text.equals(autoClosingPair.getKey()) && isFollowedBy(document, command.offset, autoClosingPair.getKey())) {
            command.text = "";
        } else if (command.text.equals(autoClosingPair.getValue()) && isFollowedBy(document, command.offset, autoClosingPair.getValue())) {
            command.text = "";
        } else {
            command.text += autoClosingPair.getValue();
        }
        return;
    }
    Arrays.stream(contentTypes).flatMap(contentType -> registry.getEnabledAutoClosingPairs(contentType).stream()).map(CharacterPair::getValue).filter(command.text::equals).filter(closing -> isFollowedBy(document, command.offset, closing)).findFirst().ifPresent(closing -> {
        command.caretOffset = command.offset + command.text.length();
        command.shiftsCaret = false;
        command.text = "";
    });
}
Also used : DocumentCommand(org.eclipse.jface.text.DocumentCommand) Arrays(java.util.Arrays) PlatformUI(org.eclipse.ui.PlatformUI) DefaultIndentLineAutoEditStrategy(org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy) IContentType(org.eclipse.core.runtime.content.IContentType) ContentTypeHelper(org.eclipse.tm4e.ui.utils.ContentTypeHelper) CoreException(org.eclipse.core.runtime.CoreException) LanguageConfigurationRegistryManager(org.eclipse.tm4e.languageconfiguration.internal.LanguageConfigurationRegistryManager) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IDocument(org.eclipse.jface.text.IDocument) EnterAction(org.eclipse.tm4e.languageconfiguration.internal.supports.EnterAction) CharacterPair(org.eclipse.tm4e.languageconfiguration.internal.supports.CharacterPair) BadLocationException(org.eclipse.jface.text.BadLocationException) TextUtils(org.eclipse.tm4e.languageconfiguration.internal.utils.TextUtils) ITextViewer(org.eclipse.jface.text.ITextViewer) TabSpacesInfo(org.eclipse.tm4e.languageconfiguration.internal.utils.TabSpacesInfo) EnterActionAndIndent(org.eclipse.tm4e.languageconfiguration.internal.supports.EnterActionAndIndent) IAutoEditStrategy(org.eclipse.jface.text.IAutoEditStrategy) ContentTypeInfo(org.eclipse.tm4e.ui.utils.ContentTypeInfo) IEditorPart(org.eclipse.ui.IEditorPart) LanguageConfigurationRegistryManager(org.eclipse.tm4e.languageconfiguration.internal.LanguageConfigurationRegistryManager) IContentType(org.eclipse.core.runtime.content.IContentType) CharacterPair(org.eclipse.tm4e.languageconfiguration.internal.supports.CharacterPair)

Example 4 with DocumentCommand

use of org.eclipse.jface.text.DocumentCommand in project Pydev by fabioz.

the class PyEditConfigurationWithoutEditor method getAutoEditStrategies.

/**
 * Cache the result, because we'll get asked for it multiple times Now, we always return the PyAutoIndentStrategy. (even on commented lines).
 *
 * @return PyAutoIndentStrategy which deals with spaces/tabs
 */
@Override
public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
    if (autoEditStrategyWrapper == null) {
        final PyAutoIndentStrategy pyAutoEditStrategy = getPyAutoIndentStrategy(null);
        autoEditStrategyWrapper = new IAutoEditStrategy() {

            @Override
            public void customizeDocumentCommand(IDocument document, final DocumentCommand command) {
                IDocumentCommand wrapper = new IDocumentCommand() {

                    @Override
                    public void setText(String string) {
                        command.text = string;
                    }

                    @Override
                    public void setShiftsCaret(boolean b) {
                        command.shiftsCaret = b;
                    }

                    @Override
                    public void setOffset(int i) {
                        command.offset = i;
                    }

                    @Override
                    public void setLength(int i) {
                        command.length = i;
                    }

                    @Override
                    public void setCaretOffset(int i) {
                        command.caretOffset = i;
                    }

                    @Override
                    public String getText() {
                        return command.text;
                    }

                    @Override
                    public int getOffset() {
                        return command.offset;
                    }

                    @Override
                    public int getLength() {
                        return command.length;
                    }

                    @Override
                    public boolean getDoIt() {
                        return command.doit;
                    }
                };
                pyAutoEditStrategy.customizeDocumentCommand(document, wrapper);
            }
        };
    }
    return new IAutoEditStrategy[] { autoEditStrategyWrapper };
}
Also used : IAutoEditStrategy(org.eclipse.jface.text.IAutoEditStrategy) IDocumentCommand(org.python.pydev.shared_core.utils.IDocumentCommand) DocumentCommand(org.eclipse.jface.text.DocumentCommand) IDocumentCommand(org.python.pydev.shared_core.utils.IDocumentCommand) PyAutoIndentStrategy(org.python.pydev.core.autoedit.PyAutoIndentStrategy) IDocument(org.eclipse.jface.text.IDocument)

Example 5 with DocumentCommand

use of org.eclipse.jface.text.DocumentCommand in project osate2 by osate.

the class AutoUnindentEditStrategy method internalCustomizeDocumentCommand.

@Override
protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command) throws BadLocationException {
    if (!isAutoComplete() || !isLineDelimiter(document, command)) {
        return;
    }
    String publicWord = isUseCapitalization() ? "PUBLIC" + System.lineSeparator() + "\t" : "public";
    String endWord = isUseCapitalization() ? "END" : "end";
    int lineNr = document.getLineOfOffset(command.offset);
    int firstOffsetOfLine = document.getLineOffset(lineNr);
    int lineLength = document.getLineLength(lineNr);
    String lineText = document.get(firstOffsetOfLine, lineLength);
    String[] tokens = lineText.trim().split("\\s+");
    if (tokens.length < 2) {
        return;
    }
    String keyword = findKeyWord(tokens);
    boolean hasExtends = false;
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i].equalsIgnoreCase("extends")) {
            hasExtends = true;
        }
    }
    NamedElement namedElement = null;
    if (keyword.equals("")) {
        namedElement = null;
        if (document instanceof IXtextDocument) {
            IXtextDocument xDoc = (IXtextDocument) document;
            namedElement = xDoc.readOnly(resource -> {
                EObjectAtOffsetHelper helper = new EObjectAtOffsetHelper();
                EObject eobj = helper.resolveElementAt(resource, command.offset);
                if (eobj == null) {
                    return null;
                } else if (eobj instanceof Classifier || eobj instanceof AadlPackage) {
                    return (NamedElement) eobj;
                }
                return null;
            });
        }
    }
    String elementId = findElementId(tokens, keyword);
    if (namedElement == null && keyword.equals("") && !hasExtends) {
        return;
    } else if (checkForExistingEnd(endWord, hasExtends, document.get(), elementId, namedElement, keyword, tokens, firstOffsetOfLine)) {
        return;
    }
    if (ComponentCategory.getByName(elementId.toLowerCase()) != null) {
        return;
    }
    if (keyword == null || keyword.equals("") || elementId == null || elementId.equals("")) {
        return;
    }
    int firstTokenIndex = lineText.indexOf(tokens[0]);
    String leadingString = lineText.substring(0, firstTokenIndex);
    String indent = "";
    String targetString = "";
    if (keyword.equalsIgnoreCase("package")) {
        if (lineText.endsWith(System.lineSeparator())) {
            lineText = lineText.substring(0, lineText.indexOf(System.lineSeparator()));
        }
    } else if (keyword.equalsIgnoreCase("property set")) {
        if (lineText.endsWith(System.lineSeparator())) {
            lineText = lineText.substring(0, lineText.indexOf(System.lineSeparator()));
        }
        if (isAutoIndent()) {
            indent = "\t";
        }
    }
    targetString = buildTargetString(lineText, leadingString, endWord, elementId, keyword, publicWord, indent);
    if (keyword.equalsIgnoreCase("package")) {
        if (isUseCapitalization()) {
            command.text = "";
        }
        command.offset = command.offset + (System.lineSeparator() + leadingString + publicWord).length();
    } else if (keyword.equalsIgnoreCase("property set")) {
        command.offset = command.offset + (System.lineSeparator() + leadingString + indent).length();
        command.text = "";
    }
    document.replace(firstOffsetOfLine, lineText.length(), targetString);
}
Also used : DocumentCommand(org.eclipse.jface.text.DocumentCommand) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Inject(com.google.inject.Inject) EObject(org.eclipse.emf.ecore.EObject) AadlPackage(org.osate.aadl2.AadlPackage) EObjectAtOffsetHelper(org.eclipse.xtext.resource.EObjectAtOffsetHelper) Logger(org.apache.log4j.Logger) IDocument(org.eclipse.jface.text.IDocument) AbstractTerminalsEditStrategy(org.eclipse.xtext.ui.editor.autoedit.AbstractTerminalsEditStrategy) Classifier(org.osate.aadl2.Classifier) ComponentCategory(org.osate.aadl2.ComponentCategory) BadLocationException(org.eclipse.jface.text.BadLocationException) IIndentationInformation(org.eclipse.xtext.formatting.IIndentationInformation) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) TextUtilities(org.eclipse.jface.text.TextUtilities) NamedElement(org.osate.aadl2.NamedElement) MembersInjector(com.google.inject.MembersInjector) OsateCorePlugin(org.osate.core.OsateCorePlugin) AadlPackage(org.osate.aadl2.AadlPackage) EObjectAtOffsetHelper(org.eclipse.xtext.resource.EObjectAtOffsetHelper) EObject(org.eclipse.emf.ecore.EObject) Classifier(org.osate.aadl2.Classifier) NamedElement(org.osate.aadl2.NamedElement) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Aggregations

DocumentCommand (org.eclipse.jface.text.DocumentCommand)10 BadLocationException (org.eclipse.jface.text.BadLocationException)5 IDocument (org.eclipse.jface.text.IDocument)5 MultiLineTerminalsEditStrategy (org.eclipse.xtext.ui.editor.autoedit.MultiLineTerminalsEditStrategy)3 AbstractXtextDocumentTest (org.eclipse.xtext.ui.tests.editor.model.AbstractXtextDocumentTest)3 Test (org.junit.Test)3 Inject (com.google.inject.Inject)2 MembersInjector (com.google.inject.MembersInjector)2 Logger (org.apache.log4j.Logger)2 EObject (org.eclipse.emf.ecore.EObject)2 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)2 IAutoEditStrategy (org.eclipse.jface.text.IAutoEditStrategy)2 TextUtilities (org.eclipse.jface.text.TextUtilities)2 IIndentationInformation (org.eclipse.xtext.formatting.IIndentationInformation)2 EObjectAtOffsetHelper (org.eclipse.xtext.resource.EObjectAtOffsetHelper)2 AbstractTerminalsEditStrategy (org.eclipse.xtext.ui.editor.autoedit.AbstractTerminalsEditStrategy)2 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)2 Classifier (org.osate.aadl2.Classifier)2 OsateCorePlugin (org.osate.core.OsateCorePlugin)2 Arrays (java.util.Arrays)1