Package org.eclipse.ltk.core.refactoring

Examples of org.eclipse.ltk.core.refactoring.TextFileChange


          return preview;
        }
      }
    }else{
      if (change instanceof TextFileChange){
        TextFileChange text = (TextFileChange)change;
        String path = text.getFile().getLocation()
          .toOSString().replace('\\', '/');
        if (path.equals(file)){
          return text.getPreviewContent(new NullProgressMonitor());
        }
      }
    }
    return null;
  }
View Full Code Here


            for (IFile drlFile : drlFiles) {

                if ((content = FileUtil.readFile(drlFile))==null)
                    return null;

                TextFileChange change = new TextFileChange(drlFile.getName(), drlFile);
                MultiTextEdit mte = new MultiTextEdit();
                change.setEdit(mte);

                // rename the field name
                Matcher matcher = fieldPattern.matcher(content);
                while (matcher.find()) {
                  if (isFieldInRightType(content, typeName, matcher.start())) {
                    ReplaceEdit replace = new ReplaceEdit(matcher.start(), currentName.length(), newName);
                    mte.addChild(replace);
                  }
                }

                // search all the variables of the type to replace the getters/setters
                matcher = typePattern.matcher(content);
                while (matcher.find()) {
                    if (matcher.group().length() > 0) {
                        String variableNameAssigned = matcher.group();
                        if (renameFieldProcessor.getRenameGetter()) {
                            String newGetterName = renameFieldProcessor.getNewGetterName();
                            String currentGetterName = renameFieldProcessor.getGetter().getElementName();
                            String regexp = GETTER_NAME.replace("VARIABLE_NAME", variableNameAssigned).replace("CURRENT_GETTER_NAME", currentGetterName);
                            createFieldRenameChanges(mte, content, regexp, currentGetterName, newGetterName);
                        }
                        if (renameFieldProcessor.getRenameSetter()) {
                            String newSetterName = renameFieldProcessor.getNewSetterName();
                            String currentSetterName = renameFieldProcessor.getSetter().getElementName();
                            String regexp = SETTER_NAME.replace("VARIABLE_NAME", variableNameAssigned).replace("CURRENT_SETTER_NAME", currentSetterName);
                            createFieldRenameChanges(mte, content, regexp, currentSetterName, newSetterName);
                        }
                    }
                }

                if (change.getEdit().getChildrenSize() > 0)
                    changes.add(change);

            }
        }
        return (changes.getChildren().length > 0)?changes:null;
View Full Code Here

    // false, null);
    // SourceModuleRewrite rewriter= new SourceModuleRewrite(cu, cuNode);
    Assert.isNotNull(cu);
    Assert.isNotNull(modelElements);
    Assert.isNotNull(manager);
    TextFileChange textFileChange = null;
    if (cu != null && cu.getResource() instanceof IFile) {
      textFileChange = new TextFileChange(cu.getElementName(),
          (IFile) cu.getResource());
      MultiTextEdit fileChangeRootEdit = new MultiTextEdit();
      textFileChange.setEdit(fileChangeRootEdit);

      manager.manage(cu, textFileChange);

      IModelElement[] elements = (IModelElement[]) modelElements
          .toArray(new IModelElement[modelElements.size()]);

      for (int cnt = 0, max = elements.length; cnt < max; cnt++) {
        ISourceRange sourceRange = null;
        if (elements[cnt] instanceof IMember) {
          IMember type = (IMember) elements[cnt];
          sourceRange = type.getSourceRange();
        }
        if (sourceRange != null) {
          IStructuredDocument document = determineDocument(cu);
          int suffixLength = getSuffixLength(document,
              sourceRange.getOffset() + sourceRange.getLength(),
              ';');
          int length = sourceRange.getLength() + suffixLength;
          if (sourceRange.getOffset() + length > document.getLength()) {
            length = document.getLength() - sourceRange.getOffset();
          }
          DeleteEdit edit = new DeleteEdit(sourceRange.getOffset(),
              length);

          fileChangeRootEdit.addChild(edit);
          if (cu.isWorkingCopy()) {
            textFileChange.setSaveMode(TextFileChange.LEAVE_DIRTY);
          }

        }
      }
    }
View Full Code Here

    }
    final IASTTranslationUnit ast = rootNode.getTranslationUnit();
    final String source = ast.getRawSignature();
    final ITranslationUnit tu = ast.getOriginatingTranslationUnit();
    formatChangedCode(source, tu);
    final TextFileChange subchange = ASTRewriteAnalyzer.createCTextFileChange((IFile) tu.getResource());
    subchange.setEdit(rootEdit);
    change.add(subchange);
  }
View Full Code Here

            //check the text changes
            HashSet<ASTEntry> astEntries = filterAstEntries(entry.getValue(), AST_ENTRIES_FILTER_TEXT);
            if (astEntries.size() > 0) {
                IDocument docFromResource = FileUtilsFileBuffer.getDocFromResource(workspaceFile);
                TextFileChange fileChange = new PyTextFileChange("RenameChange: " + inputName, workspaceFile);

                MultiTextEdit rootEdit = new MultiTextEdit();
                fileChange.setEdit(rootEdit);
                fileChange.setKeepPreviewEdits(true);

                List<Tuple<TextEdit, String>> renameEdits = getAllRenameEdits(docFromResource, astEntries);
                fillEditsInDocChange(fileChange, rootEdit, renameEdits);
            }
View Full Code Here

    @SuppressWarnings("unchecked")
    private TextChange createFileChange(IFile file, Pattern pattern, Collection/*FileMatch*/matches,
            RefactoringStatus resultingStatus, Collection matchGroups) throws PatternSyntaxException, CoreException {
        PositionTracker tracker = InternalSearchUI.getInstance().getPositionTracker();

        TextFileChange change = new PyTextFileChange(Messages.format(
                SearchMessages.ReplaceRefactoring_group_label_change_for_file, file.getName()), file);
        change.setEdit(new MultiTextEdit());

        ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
        manager.connect(file.getFullPath(), LocationKind.IFILE, null);
        try {
            ITextFileBuffer textFileBuffer = manager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
            if (textFileBuffer == null) {
                resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_accessing_file_buffer,
                        file.getName()));
                return null;
            }
            IDocument document = textFileBuffer.getDocument();
            String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);

            for (Iterator iterator = matches.iterator(); iterator.hasNext();) {
                FileMatch match = (FileMatch) iterator.next();
                int offset = match.getOffset();
                int length = match.getLength();
                Position currentPosition = tracker.getCurrentPosition(match);
                if (currentPosition != null) {
                    offset = currentPosition.offset;
                    if (length != currentPosition.length) {
                        resultingStatus.addError(Messages.format(
                                SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
                        continue;
                    }
                }

                String originalText = getOriginalText(document, offset, length);
                if (originalText == null) {
                    resultingStatus.addError(Messages.format(
                            SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
                    continue;
                }

                String replacementString = computeReplacementString(pattern, originalText, fReplaceString,
                        lineDelimiter);
                if (replacementString == null) {
                    resultingStatus.addError(Messages.format(
                            SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
                    continue;
                }

                ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, replacementString);
                change.addEdit(replaceEdit);
                TextEditChangeGroup textEditChangeGroup = new TextEditChangeGroup(change, new TextEditGroup(
                        SearchMessages.ReplaceRefactoring_group_label_match_replace, replaceEdit));
                change.addTextEditChangeGroup(textEditChangeGroup);
                matchGroups.add(new MatchGroup(textEditChangeGroup, match));
            }
        } finally {
            manager.disconnect(file.getFullPath(), LocationKind.IFILE, null);
        }
View Full Code Here

    // tree but I haven't found how to do this yet.
    setEnabled(path.contains("test/"));
  }

  private Change createChange(IResource resource) {
    TextFileChange result = null;
    // creation of DOM/AST from a ICompilationUnit
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    // We need the binding to be able to retrieve the types of the expressions in
    // the visitor (this is used to determine whether the assert() and fail() method
    // invocations can be found on the AssertJUnit class or not. If not, we don't
    // rewrite them since they are probably defined on the super class.
    parser.setResolveBindings(true);
    parser.setSource((ICompilationUnit) JavaCore.create(resource));
    CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);

//    AST ast = context.getASTRoot().getAST();
    AST ast = astRoot.getAST();
    ASTRewrite rewriter = new AnnotationRewriter().createRewriter(astRoot, ast);
    try {
      TextEdit edit = rewriter.rewriteAST();
      result = new TextFileChange(resource.getName(), (IFile) resource);
      result.setEdit(edit);
    } catch (JavaModelException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
View Full Code Here

        if (edits != null) {
          multiEdit.addChildren(edits.toArray(new TextEdit[edits.size()]));
        }
      }
      if (multiEdit.hasChildren()) {
        TextFileChange change = new TextFileChange("", file);
        change.setEdit(multiEdit);
        for (TextEdit e : multiEdit.getChildren()) {
          change.addTextEditGroup(new TextEditGroup("Rename constructor argument", e));
        }
        return change;
      }
    }
    catch (IOException e) {
View Full Code Here

            multiEdit.addChildren(edits.toArray(new TextEdit[edits.size()]));
          }
        }
      }
      if (multiEdit.hasChildren()) {
        TextFileChange change = new TextFileChange("", file);
        change.setEdit(multiEdit);
        for (TextEdit e : multiEdit.getChildren()) {
          change.addTextEditGroup(new TextEditGroup("Rename Bean property name", e));
        }
        return change;
      }
    }
    catch (IOException e) {
View Full Code Here

      if (model != null) {
        model.releaseFromRead();
        model = null;
      }

      TextFileChange refChanges = null;
      if (updateReferences) {
        refChanges = createRenameBeanRefsChange(file, descriptor, oldBeanId, newBeanId, monitor);
      }

      if (multiEdit.hasChildren()) {
        TextFileChange change = new TextFileChange("", file);
        change.setEdit(multiEdit);
        for (TextEdit e : multiEdit.getChildren()) {
          change.addTextEditGroup(new TextEditGroup("Rename " + descriptor.getType() + " id", e));
        }
        if (refChanges != null) {
          MultiTextEdit edit = (MultiTextEdit) refChanges.getEdit();
          if (edit.hasChildren()) {
            for (TextEdit e : edit.getChildren()) {
              edit.removeChild(e);
              multiEdit.addChild(e);
              change.addTextEditGroup(new TextEditGroup("Rename " + descriptor.getType() + " reference", e));
            }
          }
        }
        return change;
      }
View Full Code Here

TOP

Related Classes of org.eclipse.ltk.core.refactoring.TextFileChange

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.