本文整理汇总了Java中org.eclipse.text.edits.TextEdit.apply方法的典型用法代码示例。如果您正苦于以下问题:Java TextEdit.apply方法的具体用法?Java TextEdit.apply怎么用?Java TextEdit.apply使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.text.edits.TextEdit
的用法示例。
在下文中一共展示了TextEdit.apply方法的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: 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();
}
示例4: 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());
}
示例5: 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 TypeScript {@link CodeEdit}.
* @throws TypeScriptException
* @throws BadLocationException
* @throws MalformedTreeException
*/
public static void applyEdits(IDocument document, List<CodeEdit> codeEdits)
throws TypeScriptException, MalformedTreeException, BadLocationException {
if (document == null || codeEdits.isEmpty()) {
return;
}
IDocumentUndoManager manager = DocumentUndoManagerRegistry.getDocumentUndoManager(document);
if (manager != null) {
manager.beginCompoundChange();
}
try {
TextEdit edit = toTextEdit(codeEdits, document);
// RewriteSessionEditProcessor editProcessor = new
// RewriteSessionEditProcessor(document, edit,
// org.eclipse.text.edits.TextEdit.NONE);
// editProcessor.performEdits();
edit.apply(document);
} finally {
if (manager != null) {
manager.endCompoundChange();
}
}
}
示例6: processInlineLicense
import org.eclipse.text.edits.TextEdit; //导入方法依赖的package包/类
private void processInlineLicense(IDocument doc) throws Exception {
CompilationUnit cu = getAST(doc);
cu.recordModifications();
AST ast = cu.getAST();
if (cu.types().get(0) instanceof TypeDeclaration) {
TypeDeclaration td = (TypeDeclaration)cu.types().get(0);
FieldDeclaration[] fd = td.getFields();
if (fd.length == 0) {
td.bodyDeclarations().add(0, createLiceseInLineField(ast));
} else {
FieldDeclaration firstFd = fd[0];
VariableDeclarationFragment vdf = (VariableDeclarationFragment)firstFd.fragments().get(0);
if (vdf.getName().getIdentifier().equals("COPYRIGHT")) {
td.bodyDeclarations().remove(0);
td.bodyDeclarations().add(0, createLiceseInLineField(ast));
} else {
td.bodyDeclarations().add(0, createLiceseInLineField(ast));
}
}
}
//record changes
TextEdit edits = cu.rewrite(doc, null);
edits.apply(doc);
}
示例7: 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;
}
示例8: format
import org.eclipse.text.edits.TextEdit; //导入方法依赖的package包/类
private void format(IDocument doc, CompilationUnitContext context) throws BadLocationException {
Map<String, String> options;
IJavaProject project = context.getJavaProject();
if (project != null) options = project.getOptions(true);
else options = JavaCore.getOptions();
String contents = doc.get();
int[] kinds = {CodeFormatter.K_EXPRESSION, CodeFormatter.K_STATEMENTS, CodeFormatter.K_UNKNOWN};
TextEdit edit = null;
for (int i = 0; i < kinds.length && edit == null; i++) {
edit =
CodeFormatterUtil.format2(
kinds[i], contents, fInitialIndentLevel, fLineDelimiter, options);
}
if (edit == null) throw new BadLocationException(); // fall back to indenting
edit.apply(doc, TextEdit.UPDATE_REGIONS);
}
示例9: format
import org.eclipse.text.edits.TextEdit; //导入方法依赖的package包/类
/**
* Old API. Consider to use format2 (TextEdit)
*
* @param kind Use to specify the kind of the code snippet to format. It can be any of the kind
* constants defined in {@link org.eclipse.jdt.core.formatter.CodeFormatter}
* @param source The source to format
* @param indentationLevel The initial indentation level, used to shift left/right the entire
* source fragment. An initial indentation level of zero or below has no effect.
* @param lineSeparator The line separator to use in formatted source, if set to <code>null</code>
* , then the platform default one will be used.
* @param options The options map to use for formatting with the default code formatter.
* Recognized options are documented on {@link
* org.eclipse.jdt.core.JavaCore#getDefaultOptions()}. If set to <code>null</code>, then use
* the current settings from {@link org.eclipse.jdt.core.JavaCore#getOptions()}.
* @return the formatted source string
*/
public static String format(
int kind,
String source,
int indentationLevel,
String lineSeparator,
Map<String, String> options) {
TextEdit edit = format2(kind, source, indentationLevel, lineSeparator, options);
if (edit == null) {
return source;
} else {
Document document = new Document(source);
try {
edit.apply(document, TextEdit.NONE);
} catch (BadLocationException e) {
JavaPlugin.log(e); // bug in the formatter
Assert.isTrue(
false,
"Formatter created edits with wrong positions: " + e.getMessage()); // $NON-NLS-1$
}
return document.get();
}
}
示例10: reformatJavaSourceAsString
import org.eclipse.text.edits.TextEdit; //导入方法依赖的package包/类
/**
* Given a String containing the text of a Java source file, return the same
* Java source, but reformatted by the Eclipse auto-format code, with the
* user's current Java preferences.
*/
public static String reformatJavaSourceAsString(String source) {
TextEdit reformatTextEdit = CodeFormatterUtil.format2(
CodeFormatter.K_COMPILATION_UNIT, source, 0, (String) null,
JavaCore.getOptions());
if (reformatTextEdit != null) {
Document document = new Document(source);
try {
reformatTextEdit.apply(document, TextEdit.NONE);
source = document.get();
} catch (BadLocationException ble) {
CorePluginLog.logError(ble);
}
}
return source;
}
示例11: formatJava
import org.eclipse.text.edits.TextEdit; //导入方法依赖的package包/类
private String formatJava(IType type) throws JavaModelException {
String source = type.getCompilationUnit().getSource();
CodeFormatter formatter = ToolFactory.createCodeFormatter(type.getJavaProject().getOptions(true));
TextEdit formatEdit = formatter.format(CodeFormatterFlags.getFlagsForCompilationUnitFormat(), source, 0,
source.length(), 0, lineDelimiter);
if (formatEdit == null) {
CorePluginLog.logError("Could not format source for " + type.getCompilationUnit().getElementName());
return source;
}
Document document = new Document(source);
try {
formatEdit.apply(document);
source = document.get();
} catch (BadLocationException e) {
CorePluginLog.logError(e);
}
source = Strings.trimLeadingTabsAndSpaces(source);
return source;
}
示例12: commitCodeChange
import org.eclipse.text.edits.TextEdit; //导入方法依赖的package包/类
public void commitCodeChange(ICompilationUnit iCompilationUnit, ASTRewrite rewriter) {
try {
Document document = new Document(iCompilationUnit.getSource());
TextEdit edits = rewriter.rewriteAST(document, null);
edits.apply(document);
iCompilationUnit.getBuffer().setContents(document.get());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例13: rewriteCompilationUnit
import org.eclipse.text.edits.TextEdit; //导入方法依赖的package包/类
/**
* Rewrites compilation unit with new source
*
* @param unit
* compilation unit to be rewritten
* @param source
* new source of compilation unit
*/
private void rewriteCompilationUnit(final ICompilationUnit unit, final String source) {
try {
final TextEdit edits = rewriter.rewriteAST();
final Document document = new Document(source);
edits.apply(document);
unit.getBuffer().setContents(document.get());
change.setEdit(edits);
} catch (final JavaModelException | MalformedTreeException | BadLocationException e) {
ConsoleUtils.printError(e.getMessage());
}
}
示例14: asFormattedString
import org.eclipse.text.edits.TextEdit; //导入方法依赖的package包/类
public static String asFormattedString(ASTNode node, int indent, String lineDelim, Map<String, String> options) {
String unformatted= asString(node);
TextEdit edit= CodeFormatterUtil.format2(node, unformatted, indent, lineDelim, options);
if (edit != null) {
Document document= new Document(unformatted);
try {
edit.apply(document, TextEdit.NONE);
} catch (BadLocationException e) {
// bug in the formatter
JavaManipulationPlugin.log(e);
}
return document.get();
}
return unformatted; // unknown node
}
示例15: doCommand
import org.eclipse.text.edits.TextEdit; //导入方法依赖的package包/类
@Override
protected void doCommand(IDocument document) throws BadLocationException {
ISourceRange srTarget = target.getSourceRange();
int insertOffset = srTarget.getOffset(); // BEFORE
if (location == Insert.AFTER) {
insertOffset += target.getLengthWithSep();
}
int docLen = document.getLength();
String prefix = "";
if (insertOffset > docLen) {
insertOffset -= target.getLineDelim().length();
prefix = target.getLineDelim();
}
TextEdit edit = new MultiTextEdit();
String suffix = "";
for (PagePart part : parts) {
ISourceRange range = part.getSourceRange();
int offset = range.getOffset();
int len = part.getLengthWithSep();
if (offset + len > docLen) {
len -= target.getLineDelim().length();
suffix = target.getLineDelim();
}
String text = prefix + document.get(offset, len) + suffix;
edit.addChild(new DeleteEdit(offset, len));
edit.addChild(new InsertEdit(insertOffset, text));
}
edit.apply(document);
}