use of org.eclipse.text.edits.MalformedTreeException in project webtools.sourceediting by eclipse.
the class SurroundWithNewElementQuickAssistProposal 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) {
try {
int startTagOffset = offset;
int endTagOffset = offset + viewer.getSelectedRange().y;
// surround the node if no selection
if (startTagOffset == endTagOffset) {
IDOMNode cursorNode = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, offset);
// use parent node if text node is empty
if ((cursorNode.getNodeType() == Node.TEXT_NODE) && (cursorNode.getNodeValue().trim().length() == 0)) {
cursorNode = (IDOMNode) cursorNode.getParentNode();
}
startTagOffset = cursorNode.getStartOffset();
endTagOffset = cursorNode.getEndOffset();
}
// insert new element
MultiTextEdit multiTextEdit = new MultiTextEdit();
// element tag name cannot be DBCS, do not translate "<element>"
// and "</element>"
// $NON-NLS-1$ //$NON-NLS-2$
final String startElement = "<" + ELEMENT_NAME + ">";
multiTextEdit.addChild(new InsertEdit(startTagOffset, startElement));
// $NON-NLS-1$ //$NON-NLS-2$
multiTextEdit.addChild(new InsertEdit(endTagOffset, "</" + ELEMENT_NAME + ">"));
multiTextEdit.apply(viewer.getDocument());
Position start = new Position(startTagOffset);
Position end = new Position(endTagOffset + startElement.length());
try {
viewer.getDocument().addPosition(start);
viewer.getDocument().addPosition(end);
// get new element node
IDOMNode newElementNode = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, startTagOffset);
IStructuredFormatProcessor formatProcessor = new FormatProcessorXML();
formatProcessor.formatNode(newElementNode);
// rename new element
apply(viewer, trigger, stateMask, start, end, ELEMENT_NAME.length());
} finally {
viewer.getDocument().removePosition(start);
viewer.getDocument().removePosition(end);
}
} catch (MalformedTreeException e) {
// log for now, unless find reason not to
Logger.log(Logger.INFO, e.getMessage());
} catch (BadLocationException e) {
// log for now, unless find reason not to
Logger.log(Logger.INFO, e.getMessage());
}
}
use of org.eclipse.text.edits.MalformedTreeException in project webtools.sourceediting by eclipse.
the class AddJavaDocStubOperation method applyChanges.
protected void applyChanges() {
IModelManager modelManager = StructuredModelManager.getModelManager();
IStructuredModel model = null;
IStructuredDocument doc = node.getStructuredDocument();
try {
MultiTextEdit edits = textEditListener.getTextEdits();
model = modelManager.getExistingModelForEdit(doc);
model.aboutToChangeModel();
// $NON-NLS-1$ //$NON-NLS-2$
model.beginRecording(this, Messages.getString("AddJavaDocStubOperation.0"), Messages.getString("AddJavaDocStubOperation.1"));
edits.apply(doc);
} catch (MalformedTreeException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
} catch (BadLocationException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
} finally {
if (model != null) {
model.endRecording(this);
model.changedModel();
model.releaseFromEdit();
}
}
}
use of org.eclipse.text.edits.MalformedTreeException in project webtools.sourceediting by eclipse.
the class PageImport method insertImportDeclaration.
private int insertImportDeclaration(IDocument document, int position, boolean isXML) {
String delim = (document instanceof IStructuredDocument) ? ((IStructuredDocument) document).getLineDelimiter() : TextUtilities.getDefaultLineDelimiter(document);
boolean isCustomTag = isCustomTagDocument(document);
final String opening;
final String closing;
if (isCustomTag) {
if (isXML) {
// $NON-NLS-1$
opening = "<jsp:directive.tag import=\"";
// $NON-NLS-1$
closing = "\"/>";
} else {
// $NON-NLS-1$
opening = "<%@tag import=\"";
// $NON-NLS-1$
closing = "\"%>";
}
} else {
if (isXML) {
// $NON-NLS-1$
opening = "<jsp:directive.page import=\"";
// $NON-NLS-1$
closing = "\"/>";
} else {
// $NON-NLS-1$
opening = "<%@page import=\"";
// $NON-NLS-1$
closing = "\"%>";
}
}
final String declaration = opening + name + closing + delim;
final InsertEdit edit = new InsertEdit(position, declaration);
try {
edit.apply(document);
return declaration.length();
} catch (BadLocationException e) {
} catch (MalformedTreeException e) {
}
return -1;
}
use of org.eclipse.text.edits.MalformedTreeException in project webtools.sourceediting by eclipse.
the class JSPTranslationExtension method getJspEdit.
/**
* Returns a corresponding TextEdit for the JSP file given a TextEdit for
* a Java file.
*
* @param javaEdit
* @return the corresponding JSP edits (not applied to the document yet)
*/
public TextEdit getJspEdit(TextEdit javaEdit) {
if (javaEdit == null)
return null;
List jspEdits = new ArrayList();
int offset = javaEdit.getOffset();
int length = javaEdit.getLength();
if (javaEdit instanceof MultiTextEdit && javaEdit.getChildren().length > 0) {
IRegion r = TextEdit.getCoverage(getAllEdits(javaEdit));
offset = r.getOffset();
length = r.getLength();
}
// get java ranges that will be affected by the edit
Position[] javaPositions = getJavaRanges(offset, length);
// record position data before the change
Position[] jspPositions = new Position[javaPositions.length];
PositionDelta[] deltas = new PositionDelta[javaPositions.length];
for (int i = 0; i < javaPositions.length; i++) {
deltas[i] = new PositionDelta(javaPositions[i].offset, javaPositions[i].length);
// mapping from java <-> jsp (eg. an import statement)
if (!isIndirect(javaPositions[i].offset))
jspPositions[i] = (Position) getJava2JspMap().get(javaPositions[i]);
}
if (DEBUG) {
// $NON-NLS-1$
System.out.println("================================================");
// $NON-NLS-1$
System.out.println("deltas:");
String javaText = getJavaText();
for (int i = 0; i < deltas.length; i++) // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println("pos[" + deltas[i].preOffset + ":" + deltas[i].preLength + "]" + javaText.substring(deltas[i].preOffset, deltas[i].preOffset + deltas[i].preLength));
// $NON-NLS-1$
System.out.println("===============================================");
}
UndoEdit undo = null;
// apply the edit to the java document
try {
undo = javaEdit.apply(getJavaDocument());
} catch (MalformedTreeException e) {
Logger.logException(e);
} catch (BadLocationException e) {
Logger.logException(e);
}
// now at this point Java positions are unreliable since they were
// updated after applying java edit.
String newJavaText = getJavaDocument().get();
if (DEBUG)
// $NON-NLS-1$
System.out.println("java post format text:\n" + newJavaText);
// record post edit data
for (int i = 0; i < javaPositions.length; i++) deltas[i].setPostEditData(javaPositions[i].offset, javaPositions[i].length, javaPositions[i].isDeleted);
// create appropriate text edits for deltas
Position jspPos = null;
// $NON-NLS-1$
String replaceText = "";
for (int i = 0; i < deltas.length; i++) {
jspPos = jspPositions[i];
if (jspPos != null) {
if (deltas[i].isDeleted) {
jspEdits.add(new DeleteEdit(jspPos.offset, jspPos.length));
} else {
replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
// get rid of pre and post white space or fine tuned
// adjustment later.
// fix text here...
replaceText = fixJspReplaceText(replaceText, jspPos);
if (// Unwanted TextEdit can lead to MalformedTreeException.See: Bug 321977
!(replaceText.length() == 0 && jspPos.length == 0))
jspEdits.add(new ReplaceEdit(jspPos.offset, jspPos.length, replaceText));
}
if (DEBUG)
debugReplace(deltas, jspPos, replaceText, i);
} else {
// possible new import?
if (isImport(javaPositions[i].getOffset()) && replaceText.lastIndexOf("import ") != -1) {
// $NON-NLS-1$
replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
// $NON-NLS-1$ //$NON-NLS-2$
String importText = replaceText.substring(replaceText.lastIndexOf("import "), replaceText.indexOf(";"));
// evenutally need to check if it's XML-JSP
// $NON-NLS-1$ //$NON-NLS-2$
importText = "<%@page import=\"" + importText + "\" %>\n";
jspEdits.add(new InsertEdit(0, importText));
}
}
}
TextEdit allJspEdits = createMultiTextEdit((TextEdit[]) jspEdits.toArray(new TextEdit[jspEdits.size()]));
// editor)
if (undo != null) {
try {
undo.apply(getJavaDocument());
} catch (MalformedTreeException e) {
Logger.logException(e);
} catch (BadLocationException e) {
Logger.logException(e);
}
}
return allJspEdits;
}
use of org.eclipse.text.edits.MalformedTreeException in project liferay-ide by liferay.
the class InputContext method flushModel.
protected void flushModel(IDocument doc) {
boolean flushed = true;
if (fEditOperations.size() > 0) {
try {
MultiTextEdit edit = new MultiTextEdit();
if (isNewlineNeeded(doc)) {
insert(edit, new InsertEdit(doc.getLength(), TextUtilities.getDefaultLineDelimiter(doc)));
}
for (int i = 0; i < fEditOperations.size(); i++) {
insert(edit, (TextEdit) fEditOperations.get(i));
}
if (_fModel instanceof IEditingModel) {
((IEditingModel) _fModel).setStale(true);
}
edit.apply(doc);
fEditOperations.clear();
} catch (MalformedTreeException mte) {
LiferayUIPlugin.logError(mte);
flushed = false;
} catch (BadLocationException ble) {
LiferayUIPlugin.logError(ble);
flushed = false;
}
}
if (flushed && (_fModel instanceof IEditable)) {
((IEditable) _fModel).setDirty(false);
}
}
Aggregations