本文整理汇总了Java中org.eclipse.text.edits.TextEdit类的典型用法代码示例。如果您正苦于以下问题:Java TextEdit类的具体用法?Java TextEdit怎么用?Java TextEdit使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TextEdit类属于org.eclipse.text.edits包,在下文中一共展示了TextEdit类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: format
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
/**
* This method makes sure that no changes are applied (no dirty state), if there are no changes. This fixes bug
* GHOLD-272
*/
@Override
public void format(IDocument document, IRegion region) {
IXtextDocument doc = (IXtextDocument) document;
TextEdit e = doc.priorityReadOnly(new FormattingUnitOfWork(doc, region));
if (e == null)
return;
if (e instanceof ReplaceEdit) {
ReplaceEdit r = (ReplaceEdit) e;
if ((r.getOffset() == 0) && (r.getLength() == 0) && (r.getText().isEmpty())) {
return;
}
}
try {
e.apply(document);
} catch (BadLocationException ex) {
throw new RuntimeException(ex);
}
}
示例2: save
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
/**
* Save the AST int he Compilation Unit
*
* @param testInterface
* @param rewrite
* @throws CoreException
*/
public static void save(CompilationUnit unit, ASTRewrite rewrite) throws CoreException {
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
IPath path = unit.getJavaElement().getPath();
try {
bufferManager.connect(path, null);
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path);
IDocument document = textFileBuffer.getDocument();
TextEdit edit = rewrite.rewriteAST(document, null);
edit.apply(document);
textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */);
} catch (Exception e) {
ResourceManager.logException(e);
} finally {
// disconnect the path
bufferManager.disconnect(path, null);
}
}
示例3: applyEdits
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
/**
* Method will apply all edits to document as single modification. Needs to
* be executed in UI thread.
*
* @param document
* document to modify
* @param edits
* list of LSP TextEdits
*/
public static void applyEdits(IDocument document, TextEdit edit) {
if (document == null) {
return;
}
IDocumentUndoManager manager = DocumentUndoManagerRegistry.getDocumentUndoManager(document);
if (manager != null) {
manager.beginCompoundChange();
}
try {
RewriteSessionEditProcessor editProcessor = new RewriteSessionEditProcessor(document, edit,
org.eclipse.text.edits.TextEdit.NONE);
editProcessor.performEdits();
} catch (MalformedTreeException | BadLocationException e) {
EditorConfigPlugin.logError(e);
}
if (manager != null) {
manager.endCompoundChange();
}
}
示例4: apply
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
public String apply(String contents) {
final TextEdit edit = codeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT
| CodeFormatter.F_INCLUDE_COMMENTS, contents, 0,
contents.length(), 0, Constants.LF);
if (edit == null) {
// TODO log a fatal or warning here. Throwing an exception is causing the actual freemarker error to be lost
return contents;
}
IDocument document = new Document(contents);
try {
edit.apply(document);
} catch (Exception e) {
throw new RuntimeException(
"Failed to format the generated source code.", e);
}
return document.get();
}
示例5: formatEclipseStyle
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
public static String formatEclipseStyle(final Properties prop, final String content) {
final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(prop);
final IDocument document = new Document(content);
try {
final TextEdit textEdit =
codeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS,
content,
0,
content.length(),
0,
null);
if (textEdit != null) {
textEdit.apply(document);
} else {
return content;
}
} catch (final BadLocationException e) {
return content;
}
return ensureCorrectNewLines(document.get());
}
示例6: organizeImportsInCompilationUnit
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
public void organizeImportsInCompilationUnit(ICompilationUnit unit, WorkspaceEdit rootEdit) {
try {
InnovationContext context = new InnovationContext(unit, 0, unit.getBuffer().getLength() - 1);
CUCorrectionProposal proposal = new CUCorrectionProposal("OrganizeImports", unit, IProposalRelevance.ORGANIZE_IMPORTS) {
@Override
protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
CompilationUnit astRoot = context.getASTRoot();
OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, null);
editRoot.addChild(op.createTextEdit(null));
}
};
addWorkspaceEdit(unit, proposal, rootEdit);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem organize imports ", e);
}
}
示例7: visit
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
@Override
public boolean visit(CopyTargetEdit edit) {
try {
org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit();
te.setRange(JDTUtils.toRange(compilationUnit, edit.getOffset(), edit.getLength()));
Document doc = new Document(compilationUnit.getSource());
edit.apply(doc, TextEdit.UPDATE_REGIONS);
String content = doc.get(edit.getSourceEdit().getOffset(), edit.getSourceEdit().getLength());
te.setNewText(content);
converted.add(te);
} catch (MalformedTreeException | BadLocationException | CoreException e) {
JavaLanguageServerPlugin.logException("Error converting TextEdits", e);
}
return false; // do not visit children
}
示例8: addEdits
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
super.addEdits(doc, root);
// build a full AST
CompilationUnit unit = SharedASTProvider.getInstance().getAST(getCompilationUnit(), null);
ASTNode name= NodeFinder.perform(unit, fOffset, fLength);
if (name instanceof SimpleName) {
SimpleName[] names= LinkedNodeFinder.findByProblems(unit, (SimpleName) name);
if (names != null) {
for (int i= 0; i < names.length; i++) {
SimpleName curr= names[i];
root.addChild(new ReplaceEdit(curr.getStartPosition(), curr.getLength(), fNewName));
}
return;
}
}
root.addChild(new ReplaceEdit(fOffset, fLength, fNewName));
}
示例9: addEdits
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
super.addEdits(doc, root);
ICompilationUnit cu= getCompilationUnit();
IPackageFragment parentPack= (IPackageFragment) cu.getParent();
IPackageDeclaration[] decls= cu.getPackageDeclarations();
if (parentPack.isDefaultPackage() && decls.length > 0) {
for (int i= 0; i < decls.length; i++) {
ISourceRange range= decls[i].getSourceRange();
root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
}
return;
}
if (!parentPack.isDefaultPackage() && decls.length == 0) {
String lineDelim = "\n";
String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$
root.addChild(new InsertEdit(0, str));
return;
}
root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
示例10: addEdits
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
@Override
protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
super.addEdits(document, editRoot);
ASTRewrite rewrite= getRewrite();
if (rewrite != null) {
try {
TextEdit edit= rewrite.rewriteAST();
editRoot.addChild(edit);
} catch (IllegalArgumentException e) {
throw new CoreException(StatusFactory.newErrorStatus("Invalid AST rewriter", e));
}
}
if (fImportRewrite != null) {
editRoot.addChild(fImportRewrite.rewriteImports(new NullProgressMonitor()));
}
}
示例11: addEdits
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
@Override
protected void addEdits(IDocument document, TextEdit rootEdit) throws CoreException {
try {
String lineDelimiter= TextUtilities.getDefaultLineDelimiter(document);
final IJavaProject project= getCompilationUnit().getJavaProject();
IRegion region= document.getLineInformationOfOffset(fInsertPosition);
String lineContent= document.get(region.getOffset(), region.getLength());
String indentString= Strings.getIndentString(lineContent, project);
String str= Strings.changeIndent(fComment, 0, project, indentString, lineDelimiter);
InsertEdit edit= new InsertEdit(fInsertPosition, str);
rootEdit.addChild(edit);
if (fComment.charAt(fComment.length() - 1) != '\n') {
rootEdit.addChild(new InsertEdit(fInsertPosition, lineDelimiter));
rootEdit.addChild(new InsertEdit(fInsertPosition, indentString));
}
} catch (BadLocationException e) {
throw new CoreException(StatusFactory.newErrorStatus("Invalid edit", e));
}
}
示例12: formatPagePart
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
private static void formatPagePart(PagePart part, TextEdit edit) {
switch (part.getKind()) {
case BLANK:
formatBlank(part, edit);
break;
case TEXT:
formatText(part, edit, cols);
break;
case LIST:
formatList(part, edit, cols, tabWidth);
break;
case TABLE:
formatTable(part, edit);
break;
default:
break;
}
}
示例13: performEdits
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
/**
* Executes the text edits on the given document. Subclasses that override this method should call
* <code>super.performEdits(document)</code>.
*
* @param document the document
* @return an object representing the undo of the executed edits
* @exception MalformedTreeException is thrown if the edit tree isn't in a valid state. This
* exception is thrown before any edit is executed. So the document is still in its original
* state.
* @exception BadLocationException is thrown if one of the edits in the tree can't be executed.
* The state of the document is undefined if this exception is thrown.
* @since 3.5
*/
protected UndoEdit performEdits(IDocument document)
throws BadLocationException, MalformedTreeException {
DocumentRewriteSession session = null;
try {
if (document instanceof IDocumentExtension4) {
session =
((IDocumentExtension4) document)
.startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
}
LinkedModeModel.closeAllModels(document);
TextEditProcessor processor = createTextEditProcessor(document, TextEdit.CREATE_UNDO, false);
return processor.performEdits();
} finally {
if (session != null) {
((IDocumentExtension4) document).stopRewriteSession(session);
}
}
}
示例14: formatCode
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
private static String formatCode(String contents, Object codeFormatter) {
if (codeFormatter instanceof CodeFormatter) {
IDocument doc = new Document(contents);
TextEdit edit = ((CodeFormatter) codeFormatter).format(CodeFormatter.K_COMPILATION_UNIT, doc.get(), 0,
doc.get().length(), 0, null);
if (edit != null) {
try {
edit.apply(doc);
contents = doc.get();
} catch (Exception e) {
System.out.println(e);
}
}
}
return contents;
}
示例15: coveredBy
import org.eclipse.text.edits.TextEdit; //导入依赖的package包/类
private boolean coveredBy(TextEditBasedChangeGroup group, IRegion sourceRegion) {
int sLength = sourceRegion.getLength();
if (sLength == 0) return false;
int sOffset = sourceRegion.getOffset();
int sEnd = sOffset + sLength - 1;
TextEdit[] edits = group.getTextEdits();
for (int i = 0; i < edits.length; i++) {
TextEdit edit = edits[i];
if (edit.isDeleted()) return false;
int rOffset = edit.getOffset();
int rLength = edit.getLength();
int rEnd = rOffset + rLength - 1;
if (rLength == 0) {
if (!(sOffset < rOffset && rOffset <= sEnd)) return false;
} else {
if (!(sOffset <= rOffset && rEnd <= sEnd)) return false;
}
}
return true;
}