Search in sources :

Example 41 with ReplaceEdit

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

the class BasicRefactorSearchRequestor method acceptSearchMatch.

/**
 * @see org.eclipse.wst.jsdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.wst.jsdt.core.search.SearchMatch)
 */
public void acceptSearchMatch(SearchMatch javaMatch) throws CoreException {
    String matchDocumentPath = javaMatch.getResource().getFullPath().toString();
    SearchDocument searchDoc = JsSearchSupport.getInstance().getSearchDocument(matchDocumentPath);
    if (searchDoc != null && searchDoc instanceof JSDTSearchDocumentDelegate) {
        String renameText = getRenameText((JSDTSearchDocumentDelegate) searchDoc, javaMatch);
        // add it for the correct document
        addJavaEdit(searchDoc.getPath(), new ReplaceEdit(javaMatch.getOffset(), javaMatch.getLength(), renameText));
    }
}
Also used : JSDTSearchDocumentDelegate(org.eclipse.wst.jsdt.web.core.javascript.search.JSDTSearchDocumentDelegate) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) SearchDocument(org.eclipse.wst.jsdt.core.search.SearchDocument)

Example 42 with ReplaceEdit

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

the class RenameComponentProcessor method addDeclarationUpdate.

final void addDeclarationUpdate(TextChangeManager manager, IFile file) throws CoreException {
    String componentName = selectedComponent.getName();
    String componentNamespace = selectedComponent.getNamespaceURI();
    QualifiedName elementQName = new QualifiedName(componentNamespace, componentName);
    QualifiedName typeQName = selectedComponent.getTypeQName();
    SearchScope scope = new WorkspaceSearchScope();
    if (file != null) {
        scope = new SelectionSearchScope(new IResource[] { file });
    }
    CollectingSearchRequestor requestor = new CollectingSearchRequestor();
    SearchPattern pattern = new XMLComponentDeclarationPattern(file, elementQName, typeQName);
    SearchEngine searchEngine = new SearchEngine();
    HashMap map = new HashMap();
    if (singleFileOnly) {
        map.put("searchDirtyContent", Boolean.TRUE);
    }
    searchEngine.search(pattern, requestor, scope, map, new NullProgressMonitor());
    List results = requestor.getResults();
    // more than one declaration found, so use offset as additional check
    Position offsetPosition = null;
    if (results.size() > 1) {
        IDOMElement selectedElement = selectedComponent.getElement();
        if (selectedElement != null) {
            int startOffset = selectedElement.getStartOffset();
            offsetPosition = new Position(startOffset, (selectedElement.getEndOffset() - startOffset));
        }
    }
    for (Iterator iter = results.iterator(); iter.hasNext(); ) {
        SearchMatch match = (SearchMatch) iter.next();
        if (match != null) {
            boolean addTextChange = true;
            // additional check to verify correct declaration is changed
            if (offsetPosition != null) {
                addTextChange = offsetPosition.overlapsWith(match.getOffset(), match.getLength());
            }
            if (addTextChange) {
                TextChange textChange = manager.get(match.getFile());
                String newName = getNewElementName();
                newName = quoteString(newName);
                ReplaceEdit replaceEdit = new ReplaceEdit(match.getOffset(), match.getLength(), newName);
                String editName = RefactoringMessages.getString("RenameComponentProcessor.Component_Refactoring_update_declatation");
                TextChangeCompatibility.addTextEdit(textChange, editName, replaceEdit);
            }
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SearchMatch(org.eclipse.wst.common.core.search.SearchMatch) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) QualifiedName(org.eclipse.wst.common.core.search.pattern.QualifiedName) SelectionSearchScope(org.eclipse.wst.common.core.search.scope.SelectionSearchScope) TextChange(org.eclipse.ltk.core.refactoring.TextChange) WorkspaceSearchScope(org.eclipse.wst.common.core.search.scope.WorkspaceSearchScope) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) SearchEngine(org.eclipse.wst.common.core.search.SearchEngine) SearchScope(org.eclipse.wst.common.core.search.scope.SearchScope) SelectionSearchScope(org.eclipse.wst.common.core.search.scope.SelectionSearchScope) WorkspaceSearchScope(org.eclipse.wst.common.core.search.scope.WorkspaceSearchScope) SearchPattern(org.eclipse.wst.common.core.search.pattern.SearchPattern) Iterator(java.util.Iterator) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) ArrayList(java.util.ArrayList) List(java.util.List) CollectingSearchRequestor(org.eclipse.wst.common.core.search.util.CollectingSearchRequestor) XMLComponentDeclarationPattern(org.eclipse.wst.xml.core.internal.search.XMLComponentDeclarationPattern) IResource(org.eclipse.core.resources.IResource)

Example 43 with ReplaceEdit

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

the class DefaultXMLPartitionFormatter method indentIfNotAlreadyIndented.

/**
 * Indent if whitespaceRun does not already contain an indent
 *
 * @param textEdit
 * @param indentLevel
 * @param indentStartOffset
 * @param maxAvailableLineWidth
 * @param whitespaceRun
 * @return new available line width up to where indented
 */
private int indentIfNotAlreadyIndented(TextEdit textEdit, IStructuredDocumentRegion currentRegion, int indentLevel, int indentStartOffset, String whitespaceRun) {
    int maxAvailableLineWidth = getFormattingPreferences().getMaxLineWidth();
    int availableLineWidth;
    String indentString = getIndentString(indentLevel);
    String lineDelimiter = getLineDelimiter(currentRegion);
    String newLineAndIndent = lineDelimiter + indentString;
    TextEdit indentation = null;
    // if not already correctly indented
    if (!newLineAndIndent.equals(whitespaceRun)) {
        if (getFormattingPreferences().getClearAllBlankLines()) {
            if (whitespaceRun != null) {
                // replace existing whitespace run
                indentation = new ReplaceEdit(indentStartOffset, whitespaceRun.length(), newLineAndIndent);
            } else {
                // just insert correct indent
                indentation = new InsertEdit(indentStartOffset, newLineAndIndent);
            }
        } else // Keep the empty lines
        {
            // just insert correct indent
            if (whitespaceRun == null)
                indentation = new InsertEdit(indentStartOffset, newLineAndIndent);
            else // Need to preserve the number of empty lines, but still indent on the current line properly
            {
                String existingDelimiters = extractLineDelimiters(whitespaceRun, currentRegion);
                if (existingDelimiters != null && existingDelimiters.length() > 0) {
                    String formatted = existingDelimiters + indentString;
                    // Don't perform a replace if the formatted string is the same as the existing whitespaceRun
                    if (!formatted.equals(whitespaceRun))
                        indentation = new ReplaceEdit(indentStartOffset, whitespaceRun.length(), formatted);
                } else
                    // No blank lines to preserve - correct the indent
                    indentation = new ReplaceEdit(indentStartOffset, whitespaceRun.length(), newLineAndIndent);
            }
        }
    }
    if (indentation != null)
        textEdit.addChild(indentation);
    // update line width
    availableLineWidth = maxAvailableLineWidth - indentString.length();
    return availableLineWidth;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 44 with ReplaceEdit

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

the class DefaultXMLPartitionFormatter method collapseAndIndent.

private int collapseAndIndent(TextEdit textEdit, int spaceStartOffset, int availableLineWidth, int indentLevel, String whitespaceRun, IStructuredDocumentRegion currentRegion) {
    // Need to keep blank lines, but still collapse the whitespace
    String lineDelimiters = null;
    if (!getFormattingPreferences().getClearAllBlankLines()) {
        lineDelimiters = extractLineDelimiters(whitespaceRun, currentRegion);
        String formattedLine = lineDelimiters + getIndentString(indentLevel);
        if (lineDelimiters.length() > 0 && !formattedLine.equals(whitespaceRun)) {
            textEdit.addChild(new ReplaceEdit(spaceStartOffset, whitespaceRun.length(), formattedLine));
            availableLineWidth = getFormattingPreferences().getMaxLineWidth() - indentLevel;
        }
    }
    if (lineDelimiters == null || lineDelimiters.length() == 0) {
        availableLineWidth = collapseSpaces(textEdit, spaceStartOffset, availableLineWidth, whitespaceRun);
    }
    return availableLineWidth;
}
Also used : ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 45 with ReplaceEdit

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

the class DefaultXMLPartitionFormatter method replaceSpaces.

private int replaceSpaces(TextEdit textEdit, int spaceStartOffset, int availableLineWidth, String whitespaceRun) {
    StringBuffer buff = new StringBuffer(whitespaceRun);
    for (int i = 0; i < buff.length(); i++) {
        // $NON-NLS-1$
        buff.setCharAt(i, ' ');
    }
    String replacementString = buff.toString();
    if (!replacementString.equals(whitespaceRun)) {
        ReplaceEdit replaceEdit = new ReplaceEdit(spaceStartOffset, whitespaceRun.length(), replacementString);
        textEdit.addChild(replaceEdit);
    }
    return availableLineWidth;
}
Also used : ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

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