use of org.eclipse.text.edits.MalformedTreeException in project webtools.sourceediting by eclipse.
the class FormattingStrategyJSPJava method format.
/*
* @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#format()
*/
public void format() {
super.format();
final IDocument document = (IDocument) fDocuments.removeFirst();
final TypedPosition partition = (TypedPosition) fPartitions.removeFirst();
if (document != null && partition != null) {
try {
JSPTranslationUtil translationUtil = new JSPTranslationUtil(document);
ICompilationUnit cu = translationUtil.getCompilationUnit();
if (cu != null) {
String cuSource = cu.getSource();
/*
* Format the entire compilation unit, but only create
* edits for the requested JSP partition's range in the
* Java source
*/
TextEdit textEdit = formatString(CodeFormatter.K_COMPILATION_UNIT, cuSource, translationUtil.getTranslation().getJavaOffset(partition.getOffset()), partition.getLength(), TextUtilities.getDefaultLineDelimiter(document), getPreferences());
TextEdit jspEdit = translationUtil.getTranslation().getJspEdit(textEdit);
if (jspEdit != null && jspEdit.hasChildren())
jspEdit.apply(document);
}
} catch (MalformedTreeException exception) {
Logger.logException(exception);
} catch (BadLocationException exception) {
// Can only happen on concurrent document modification - log
// and bail out
Logger.logException(exception);
} catch (JavaModelException exception) {
Logger.logException(exception);
}
}
}
use of org.eclipse.text.edits.MalformedTreeException in project webtools.sourceediting by eclipse.
the class JSPRenameChange method perform.
/**
* Performs this change and returns a {@link JSPRenameUndoChange} to undo the change.
*
* @return a {@link JSPRenameUndoChange} to undo this performed {@link Change}
* @see org.eclipse.ltk.core.refactoring.TextChange#perform(org.eclipse.core.runtime.IProgressMonitor)
*/
public Change perform(IProgressMonitor pm) throws CoreException {
Change undoChange = null;
try {
// apply edit
undoChange = super.perform(pm);
undoChange = new JSPRenameUndoChange(this, undoChange);
// save the model
saveJSPFile(this.fJSPFile, this.getJSPDoc());
} catch (MalformedTreeException e) {
Logger.logException(e);
}
return undoChange;
}
use of org.eclipse.text.edits.MalformedTreeException in project webtools.sourceediting by eclipse.
the class BasicRefactorSearchRequestor method createChange.
/**
* </p>If a {@link TextChange} does not already exist for the given {@link JavaSearchDocumentDelegate}
* then a new one will be created with the given {@link TextEdit}. Otherwise the given {@link TextEdit}
* will be added to a new group and added to the existing change and <code>null</code> will be returned.</p>
*
* @param searchDoc the {@link JavaSearchDocumentDelegate} that the <code>edit</code> will be applied to
* @param edit the {@link TextEdit} that needs to be added to a new {@link TextChange} or appended to an
* existing one
* @param participant the {@link RefactoringParticipant} that knows about the existing {@link TextChange}s
* @return a new {@link Change} if there was not one already existing for the document in question,
* else <code>null</code>
*/
private Change createChange(JavaSearchDocumentDelegate searchDoc, TextEdit edit, RefactoringParticipant participant) {
IDocument doc = searchDoc.getJspTranslation().getJspDocument();
String description = getDescription();
TextChange existingChange = participant.getTextChange(searchDoc.getFile());
TextChange change = null;
if (existingChange != null) {
try {
existingChange.addEdit(edit);
} catch (MalformedTreeException e) {
Logger.logException(// $NON-NLS-1$
"MalformedTreeException while adding edit " + edit + " to existing change " + change, // $NON-NLS-1$
e);
}
TextEditGroup group = new TextEditGroup(description, edit);
existingChange.addTextEditGroup(group);
} else {
change = new JSPRenameChange(searchDoc.getFile(), doc, edit, searchDoc.getFile().getName());
TextEditGroup group = new TextEditGroup(description, edit);
change.addTextEditGroup(group);
}
return change;
}
use of org.eclipse.text.edits.MalformedTreeException in project webtools.sourceediting by eclipse.
the class TestPartitionFormatterXML method formatAndAssertEquals.
private void formatAndAssertEquals(String beforePath, String afterPath, XMLFormattingPreferences prefs) throws UnsupportedEncodingException, IOException, CoreException {
IStructuredModel beforeModel = null, afterModel = null;
try {
beforeModel = getModelForEdit(beforePath);
assertNotNull("could not retrieve structured model for : " + beforePath, beforeModel);
afterModel = getModelForEdit(afterPath);
assertNotNull("could not retrieve structured model for : " + afterPath, afterModel);
IStructuredDocument document = beforeModel.getStructuredDocument();
String normalizedContents = document.get();
normalizedContents = StringUtils.replace(normalizedContents, "\r\n", "\n");
normalizedContents = StringUtils.replace(normalizedContents, "\r", "\n");
document.set(normalizedContents);
if (prefs == null)
prefs = new XMLFormattingPreferences();
TextEdit edit = partitionFormatter.format(beforeModel, 0, document.getLength(), prefs);
try {
edit.apply(document);
} catch (MalformedTreeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByteArrayOutputStream formattedBytes = new ByteArrayOutputStream();
// "beforeModel" should now be
beforeModel.save(formattedBytes);
// after the formatter
ByteArrayOutputStream afterBytes = new ByteArrayOutputStream();
afterModel.save(afterBytes);
String expectedContents = new String(afterBytes.toByteArray(), UTF_8);
String actualContents = new String(formattedBytes.toByteArray(), UTF_8);
/* Make some adjustments to ignore cross platform line delimiter issues */
expectedContents = StringUtils.replace(expectedContents, "\r\n", "\n");
expectedContents = StringUtils.replace(expectedContents, "\r", "\n");
actualContents = StringUtils.replace(actualContents, "\r\n", "\n");
actualContents = StringUtils.replace(actualContents, "\r", "\n");
if (!fStringCompareUtil.equalsIgnoreLineSeperator(expectedContents, actualContents)) {
assertEquals("Formatted document differs from the expected.", expectedContents, actualContents);
}
} finally {
if (beforeModel != null)
beforeModel.releaseFromEdit();
if (afterModel != null)
afterModel.releaseFromEdit();
}
}
use of org.eclipse.text.edits.MalformedTreeException in project webtools.sourceediting by eclipse.
the class RemoveUnknownElementQuickFixProposal method apply.
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer,
* char, int, int)
*/
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
int startTagOffset = ((Integer) ((Object[]) fAdditionalFixInfo)[0]).intValue();
int startTagLength = ((Integer) ((Object[]) fAdditionalFixInfo)[1]).intValue();
int endTagOffset = ((Integer) ((Object[]) fAdditionalFixInfo)[2]).intValue();
int endTagLength = ((Integer) ((Object[]) fAdditionalFixInfo)[3]).intValue();
MultiTextEdit multiTextEdit = new MultiTextEdit();
if (endTagOffset != -1) {
multiTextEdit.addChild(new DeleteEdit(endTagOffset, endTagLength));
fSelection = new Point(endTagOffset, 0);
}
if (startTagOffset != -1) {
multiTextEdit.addChild(new DeleteEdit(startTagOffset, startTagLength));
fSelection = new Point(startTagOffset, 0);
}
try {
multiTextEdit.apply(viewer.getDocument());
} catch (MalformedTreeException e) {
// log for now, unless find reasons not to.
Logger.log(Logger.INFO, e.getMessage());
} catch (BadLocationException e) {
// log for now, unless find reasons not to.
Logger.log(Logger.INFO, e.getMessage());
}
}
Aggregations