Search in sources :

Example 36 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project titan.EclipsePlug-ins by eclipse.

the class AbstractIndentAction method setIndentLevel.

/**
 * Indents a line with a given indentation level.
 *
 * @param document
 *                the document being processed.
 * @param lineStart
 *                the offset at which the target line starts
 * @param indentationLevel
 *                the indentation level to use
 *
 * @exception BadLocationException
 *                    if the offset is invalid in this document
 */
private void setIndentLevel(final IDocument document, final int lineStart, final int indentationLevel) throws BadLocationException {
    int lastWhiteSpace = lineStart;
    char c;
    while (lastWhiteSpace < document.getLength()) {
        c = document.getChar(lastWhiteSpace);
        if (c == ' ' || c == '\t') {
            lastWhiteSpace++;
        } else {
            break;
        }
    }
    for (int i = indentArray.size() - 1; i < indentationLevel; i++) {
        indentArray.add(indentArray.get(i) + IndentationSupport.getIndentString());
    }
    if (!document.get(lineStart, lastWhiteSpace - lineStart).equals(indentArray.get(indentationLevel))) {
        multiEdit.addChild(new ReplaceEdit(lineStart, lastWhiteSpace - lineStart, indentArray.get(indentationLevel)));
    }
}
Also used : ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 37 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project titan.EclipsePlug-ins by eclipse.

the class ExtractToFunctionRefactoring method createChange.

@Override
public Change createChange(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    createFunctionText();
    createFunctionCallText();
    // 
    final TextFileChange tfc = new TextFileChange(selectionFinder.getSelectedFile().getName(), selectionFinder.getSelectedFile());
    final MultiTextEdit rootEdit = new MultiTextEdit();
    tfc.setEdit(rootEdit);
    // replace selection with function call & new declarations
    final int offset = selectionFinder.getSelectedStatements().getLocation().getOffset();
    final int len = selectionFinder.getSelectedStatements().getLocation().getEndOffset() - offset;
    rootEdit.addChild(new ReplaceEdit(offset, len, functionCallTextReady));
    // add new function after the one in which the selection is
    if (parentFunc != null && selectionFinder.getInsertLoc() >= 0) {
        rootEdit.addChild(new InsertEdit(selectionFinder.getInsertLoc(), functionTextReady));
    }
    return tfc;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 38 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.

the class DocumentChangeListenerToTextEdit method documentChanged.

public void documentChanged(DocumentEvent event) {
    int length = event.getLength();
    int offset = event.getOffset();
    String text = event.getText();
    if (length < 0) {
        return;
    }
    if (length == 0) {
        /* inserting text operation */
        InsertEdit edit = new InsertEdit(offset, text);
        textEdit.addChild(edit);
    } else if (text == null || text.equals("")) {
        // $NON-NLS-1$
        /* delete operation */
        DeleteEdit edit = new DeleteEdit(offset, length);
        textEdit.addChild(edit);
    } else if (length > 0) {
        /* replace text operation */
        ReplaceEdit edit = new ReplaceEdit(offset, length, text);
        textEdit.addChild(edit);
    }
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit)

Example 39 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.

the class JSPTranslationUtil method translateTextEdit.

public TextEdit translateTextEdit(TextEdit textEdit) {
    TextEdit translatedTextEdit = null;
    int javaOffset = textEdit.getOffset();
    int jspOffset = getTranslation().getJspOffset(textEdit.getOffset());
    int length = textEdit.getLength();
    if (textEdit instanceof MultiTextEdit) {
        translatedTextEdit = new MultiTextEdit();
        TextEdit[] children = ((MultiTextEdit) textEdit).getChildren();
        for (int i = 0; i < children.length; i++) {
            TextEdit translatedChildTextEdit = translateTextEdit(children[i]);
            if (translatedChildTextEdit != null)
                ((MultiTextEdit) translatedTextEdit).addChild(translatedChildTextEdit);
        }
    } else if (textEdit instanceof ReplaceEdit) {
        if (jspOffset == -1)
            return null;
        if (!getTranslation().javaSpansMultipleJspPartitions(javaOffset, length))
            translatedTextEdit = new ReplaceEdit(jspOffset, length, ((ReplaceEdit) textEdit).getText());
    } else if (textEdit instanceof InsertEdit) {
        translatedTextEdit = new InsertEdit(jspOffset, ((InsertEdit) textEdit).getText());
    } else if (textEdit instanceof DeleteEdit) {
        translatedTextEdit = new DeleteEdit(jspOffset, length);
        TextEdit[] children = ((DeleteEdit) textEdit).getChildren();
        for (int i = 0; i < children.length; i++) {
            TextEdit translatedChildTextEdit = translateTextEdit(children[i]);
            if (translatedChildTextEdit != null)
                ((DeleteEdit) translatedTextEdit).addChild(translatedChildTextEdit);
        }
    } else if (textEdit instanceof CopySourceEdit) {
        translatedTextEdit = new CopySourceEdit(jspOffset, length);
        ((CopySourceEdit) translatedTextEdit).setTargetEdit(((CopySourceEdit) textEdit).getTargetEdit());
        ((CopySourceEdit) translatedTextEdit).setSourceModifier(((CopySourceEdit) textEdit).getSourceModifier());
    } else if (textEdit instanceof CopyTargetEdit) {
        translatedTextEdit = new CopyTargetEdit(jspOffset);
        ((CopyTargetEdit) textEdit).getSourceEdit().setTargetEdit((CopyTargetEdit) translatedTextEdit);
    } else if (textEdit instanceof MoveSourceEdit) {
        translatedTextEdit = new MoveSourceEdit(jspOffset, length);
        ((MoveSourceEdit) translatedTextEdit).setTargetEdit(((MoveSourceEdit) textEdit).getTargetEdit());
    } else if (textEdit instanceof MoveTargetEdit) {
        translatedTextEdit = new MoveTargetEdit(jspOffset);
        ((MoveTargetEdit) textEdit).getSourceEdit().setTargetEdit((MoveTargetEdit) translatedTextEdit);
    } else {
        // $NON-NLS-1$
        System.out.println("Need to translate " + textEdit);
    }
    return translatedTextEdit;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) CopySourceEdit(org.eclipse.text.edits.CopySourceEdit) MoveTargetEdit(org.eclipse.text.edits.MoveTargetEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CopyTargetEdit(org.eclipse.text.edits.CopyTargetEdit) MoveSourceEdit(org.eclipse.text.edits.MoveSourceEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 40 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project webtools.sourceediting by eclipse.

the class FormattingStrategyJSDT method format.

/*
	 * @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#format()
	 */
public void format() {
    super.format();
    final IStructuredDocument document = (IStructuredDocument) fDocuments.removeFirst();
    final TypedPosition partition = (TypedPosition) fPartitions.removeFirst();
    if (document != null) {
        // calculate the indent of the leading <script> tag because we need to add that indent level to the JS indent level
        IStructuredDocumentRegion scriptTagStartRegion = document.getRegionAtCharacterOffset(partition.offset - 1);
        // $NON-NLS-1$
        String scriptRegionIndent = "";
        if (scriptTagStartRegion != null) {
            try {
                int scriptRegionIndentLevel = getIndentOfLine(document, document.getLineOfOffset(scriptTagStartRegion.getStartOffset())).length();
                scriptRegionIndent = getIndentationString(getPreferences(), scriptRegionIndentLevel);
                this.startIndentLevel += scriptRegionIndentLevel;
            } catch (BadLocationException e) {
                // $NON-NLS-1$
                Logger.logException("Could not calculate starting indent of the script region, using 0", e);
            }
        }
        String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
        try {
            // get the JS text from the document (not translated)
            String jsTextNotTranslated = document.get(partition.getOffset(), partition.getLength());
            String originalText = jsTextNotTranslated;
            // deal with getting the JS text and unwrapping it from any <!-- //--> statements
            String preText = "";
            String postText = lineDelim + scriptRegionIndent;
            // find and remove start comment tag if it's there
            // $NON-NLS-1$
            Pattern startPattern = Pattern.compile("(\\A(\\s*<!--.*(" + lineDelim + ")?))");
            Matcher matcher = startPattern.matcher(jsTextNotTranslated);
            if (matcher.find()) {
                preText = lineDelim + scriptRegionIndent + matcher.group().trim();
                // $NON-NLS-1$
                jsTextNotTranslated = matcher.replaceFirst("");
            }
            // find and remove end comment tag if it's there
            matcher = END_PATTERN.matcher(jsTextNotTranslated);
            if (matcher.find()) {
                // $NON-NLS-1$
                jsTextNotTranslated = matcher.replaceFirst("");
                postText = lineDelim + scriptRegionIndent + matcher.group().trim() + postText;
            }
            /*
				 * replace the text in the document with the non-translated JS
				 * text but without HTML leading and trailing comments
				 */
            int scriptLength = jsTextNotTranslated.length();
            TextEdit replaceEdit = null;
            if (scriptLength != originalText.length()) {
                replaceEdit = new ReplaceEdit(partition.getOffset(), partition.getLength(), jsTextNotTranslated);
                replaceEdit.apply(document);
            }
            // translate the web page without the script "wrapping"
            IJsTranslation translation = getTranslation(document);
            String jsTextTranslated = translation.getJsText();
            /*
				 * Set a default replace text that is the original contents
				 * with a new line and proper indentation in front
				 */
            String replaceText = lineDelim + getIndentationString(getPreferences(), startIndentLevel) + jsTextNotTranslated;
            int javaScriptOffset = ((JsTranslation) translation).getJavaScriptOffset(partition.getOffset());
            // known range, proceed
            if (javaScriptOffset >= 0) {
                // format the translated text
                TextEdit edit = CodeFormatterUtil.format2(CodeFormatter.K_JAVASCRIPT_UNIT, jsTextTranslated, javaScriptOffset, scriptLength, startIndentLevel, lineDelim, getPreferences());
                IDocument jsDoc = new Document(jsTextTranslated);
                if (edit != null) {
                    /*
						 * Put the original (possibly not JS) text back into the doc
						 * to which we're applying the edit
						 */
                    if (translation instanceof JsTranslation) {
                        IJsTranslator translator = ((JsTranslation) translation).getTranslator();
                        if (translator instanceof JsTranslator) {
                            Region[] regions = ((JsTranslator) translator).getGeneratedRanges();
                            Arrays.sort(regions, new Comparator() {

                                public int compare(Object o1, Object o2) {
                                    return ((IRegion) o1).getOffset() - ((IRegion) o2).getOffset();
                                }
                            });
                            /*
								 * for each web page range representing content needing replacements, replace it with the
								 * original web page's text
								 */
                            for (int r = 0; r < regions.length; ++r) {
                                int javascriptOffset = ((JsTranslation) translation).getJavaScriptOffset(regions[r].getOffset());
                                if (javascriptOffset > 0) {
                                    jsDoc.replace(javascriptOffset, regions[r].getLength(), document.get(regions[r].getOffset(), regions[r].getLength()));
                                }
                            }
                        }
                    }
                    edit.apply(jsDoc);
                    replaceText = lineDelim + getIndentationString(getPreferences(), startIndentLevel) + (jsDoc.get(edit.getOffset(), edit.getLength())).trim();
                } else {
                    /*
						 * Revert changes (it may still appear dirty, though,
						 * because of the above edits having been applied)
						 */
                    replaceEdit = new ReplaceEdit(partition.getOffset(), scriptLength, originalText);
                    replaceEdit.apply(document);
                    return;
                }
            }
            // apply edit to html doc using the formated translated text and the possible leading and trailing html comments
            replaceText = preText + replaceText + postText;
            replaceEdit = new ReplaceEdit(partition.getOffset(), scriptLength, replaceText);
            replaceEdit.apply(document);
        } catch (BadLocationException e) {
            Logger.logException(e);
        }
    }
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) Pattern(java.util.regex.Pattern) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) Matcher(java.util.regex.Matcher) JsTranslator(org.eclipse.wst.jsdt.web.core.javascript.JsTranslator) IJsTranslator(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslator) JsTranslation(org.eclipse.wst.jsdt.web.core.javascript.JsTranslation) IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) BasicStructuredDocument(org.eclipse.wst.sse.core.internal.text.BasicStructuredDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IJsTranslator(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslator) IRegion(org.eclipse.jface.text.IRegion) Comparator(java.util.Comparator) TypedPosition(org.eclipse.jface.text.TypedPosition) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)117 TextEdit (org.eclipse.text.edits.TextEdit)48 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)42 Test (org.junit.Test)34 InsertEdit (org.eclipse.text.edits.InsertEdit)24 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)14 IDocument (org.eclipse.jface.text.IDocument)13 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)13 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)13 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)13 UndoEdit (org.eclipse.text.edits.UndoEdit)13 BadLocationException (org.eclipse.jface.text.BadLocationException)12 DeleteEdit (org.eclipse.text.edits.DeleteEdit)12 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)10 ArrayList (java.util.ArrayList)9 List (java.util.List)7 Document (org.eclipse.jface.text.Document)7 CoreException (org.eclipse.core.runtime.CoreException)6 Position (org.eclipse.jface.text.Position)6 TextChange (org.eclipse.ltk.core.refactoring.TextChange)6