use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project liferay-ide by liferay.
the class KaleoEdits method insertAt.
public static Element insertAt(Element newElement, int offset) {
Document doc = newElement.getOwnerDocument();
if (doc instanceof IDOMDocument) {
IDOMDocument domDoc = (IDOMDocument) doc;
IndexedRegion ir = domDoc.getModel().getIndexedRegion(offset);
Node parent = ((Node) ir).getParentNode();
if (ir instanceof Text) {
Text txt = (Text) ir;
String data = txt.getData();
int dataSplitIndex = offset - ir.getStartOffset();
String beforeText = data.substring(0, dataSplitIndex);
String afterText = data.substring(dataSplitIndex);
Text after = doc.createTextNode(afterText);
Text before = doc.createTextNode(beforeText);
parent.replaceChild(after, txt);
parent.insertBefore(newElement, after);
parent.insertBefore(before, newElement);
} else if (ir instanceof Element) {
((Element) ir).appendChild(newElement);
} else {
throw new IllegalArgumentException();
}
} else {
throw new IllegalArgumentException();
}
return newElement;
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class AbstractJSONCompletionProposalComputer method computeCompletionProposals.
@Override
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
ITextViewer textViewer = context.getViewer();
int documentPosition = context.getInvocationOffset();
setErrorMessage(null);
fTextViewer = textViewer;
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition <= 0 ? 0 : documentPosition - 1);
IJSONNode node = (IJSONNode) treeNode;
while ((node != null) && (node.getNodeType() == Node.TEXT_NODE) && (node.getParentNode() != null)) {
node = node.getParentNode();
}
ContentAssistRequest contentAssistRequest = null;
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
ITextRegion completionRegion = getCompletionRegion(documentPosition, node);
// Fix completion region in case of JSON_OBJECT_CLOSE
if (completionRegion != null && completionRegion.getType() == JSONRegionContexts.JSON_OBJECT_CLOSE && documentPosition > 0) {
completionRegion = getCompletionRegion(documentPosition, node);
}
String matchString = EMPTY;
if (completionRegion != null) {
if (isPairValue(context, node)) {
try {
String nodeText = getNodeText(node);
int colonIndex = nodeText.indexOf(COLON);
int offset = documentPosition - node.getStartOffset();
if (colonIndex >= 0 && offset >= 0) {
String str = nodeText.substring(colonIndex + 1, offset);
// $NON-NLS-1$
str = str.replaceAll(",", BLANK);
matchString = str;
}
} catch (BadLocationException e) {
// ignore
}
} else {
matchString = getMatchString(sdRegion, completionRegion, documentPosition);
}
}
// compute normal proposals
contentAssistRequest = computeCompletionProposals(matchString, completionRegion, (IJSONNode) treeNode, node != null ? node.getParentNode() : null, context);
if (contentAssistRequest == null) {
contentAssistRequest = new ContentAssistRequest((IJSONNode) treeNode, node != null ? node.getParentNode() : null, sdRegion, completionRegion, documentPosition, 0, EMPTY);
setErrorMessage(JSONUIMessages.Content_Assist_not_availab_UI_);
}
/*
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=123892 Only set this
* error message if nothing else was already set
*/
if (contentAssistRequest.getProposals().size() == 0 && getErrorMessage() == null) {
setErrorMessage(JSONUIMessages.Content_Assist_not_availab_UI_);
}
ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
return (props != null) ? Arrays.asList(props) : new ArrayList(0);
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class StructureSelectAction method run.
public void run() {
Region currentRegion = new Region(fViewer.getSelectedRange().x, fViewer.getSelectedRange().y);
if (currentRegion.getLength() == fViewer.getDocument().getLength())
return;
IndexedRegion cursorIndexedRegion = getCursorIndexedRegion();
if (cursorIndexedRegion instanceof Node) {
Node cursorNode = (Node) cursorIndexedRegion;
// use parent node for empty text node
if (cursorNode.getNodeType() == Node.TEXT_NODE && cursorNode.getNodeValue().trim().length() == 0) {
cursorNode = cursorNode.getParentNode();
if (cursorNode instanceof IndexedRegion)
cursorIndexedRegion = (IndexedRegion) cursorNode;
}
Region cursorNodeRegion = new Region(cursorIndexedRegion.getStartOffset(), cursorIndexedRegion.getEndOffset() - cursorIndexedRegion.getStartOffset());
Region newRegion = null;
if (cursorNodeRegion.getOffset() >= currentRegion.getOffset() && cursorNodeRegion.getOffset() <= currentRegion.getOffset() + currentRegion.getLength() && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() >= currentRegion.getOffset() && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() <= currentRegion.getOffset() + currentRegion.getLength())
newRegion = getNewSelectionRegion(cursorNode, currentRegion);
else
newRegion = cursorNodeRegion;
if (newRegion != null) {
fHistory.remember(currentRegion);
try {
fHistory.ignoreSelectionChanges();
fEditor.selectAndReveal(newRegion.getOffset(), newRegion.getLength());
} finally {
fHistory.listenToSelectionChanges();
}
}
}
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class StructureSelectAction method getIndexedRegion.
protected IndexedRegion getIndexedRegion(int offset) {
IndexedRegion indexedRegion = null;
int lastOffset = offset;
IDocument document = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model != null) {
try {
indexedRegion = model.getIndexedRegion(lastOffset);
while (indexedRegion == null && lastOffset >= 0) {
lastOffset--;
indexedRegion = model.getIndexedRegion(lastOffset);
}
} finally {
model.releaseFromRead();
}
}
return indexedRegion;
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class StructuredSelectActionDelegate method getIndexedRegion.
/**
* This method will probably be removed and replaced by using new selection provider
* @param document
* @param offset
* @return
*/
protected IndexedRegion getIndexedRegion(IDocument document, int offset) {
IndexedRegion indexedRegion = null;
int lastOffset = offset;
IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model != null) {
try {
indexedRegion = model.getIndexedRegion(lastOffset);
while (indexedRegion == null && lastOffset >= 0) {
lastOffset--;
indexedRegion = model.getIndexedRegion(lastOffset);
}
} finally {
model.releaseFromRead();
}
}
return indexedRegion;
}
Aggregations