use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class StructuredSelectNextXMLHandler method getCursorIndexedRegion.
protected IndexedRegion getCursorIndexedRegion(IDocument document, ITextSelection textSelection) {
int offset = textSelection.getOffset() + textSelection.getLength() - 1;
if (offset < 0) {
offset = 0;
}
IndexedRegion indexedRegion = null;
indexedRegion = getIndexedRegion(document, offset);
return indexedRegion;
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method format.
public TextEdit format(IStructuredModel model, int start, int length, XMLFormattingPreferences preferences) {
setFormattingPreferences(preferences);
TextEdit edit = new MultiTextEdit();
IStructuredDocument document = model.getStructuredDocument();
// get initial document region
IStructuredDocumentRegion currentRegion = document.getRegionAtCharacterOffset(start);
if (currentRegion != null) {
int startOffset = currentRegion.getStartOffset();
// get initial dom node
IndexedRegion currentIndexedRegion = model.getIndexedRegion(startOffset);
if (currentIndexedRegion instanceof IDOMNode) {
// set up domRegion which will contain current region to be
// formatted
IDOMNode currentDOMNode = (IDOMNode) currentIndexedRegion;
DOMRegion domRegion = new DOMRegion();
domRegion.documentRegion = currentRegion;
domRegion.domNode = currentDOMNode;
XMLFormattingConstraints parentConstraints = getRegionConstraints(currentDOMNode);
/* if the whitespace strategy is declared as default, get it from the preferences */
if (XMLFormattingConstraints.DEFAULT.equals(parentConstraints.getWhitespaceStrategy()))
parentConstraints.setWhitespaceStrategy(preferences.getElementWhitespaceStrategy());
// TODO: initialize indentLevel
// initialize available line width
int lineWidth = getFormattingPreferences().getMaxLineWidth();
try {
IRegion lineInfo = document.getLineInformationOfOffset(startOffset);
lineWidth = lineWidth - (startOffset - lineInfo.getOffset());
} catch (BadLocationException e) {
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
}
parentConstraints.setAvailableLineWidth(lineWidth);
// format all siblings (and their children) as long they
// overlap with start/length
Position formatRange = new Position(start, length);
formatSiblings(edit, domRegion, parentConstraints, formatRange);
}
}
return edit;
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method computeCompletionProposals.
/**
* CONTENT ASSIST STARTS HERE
*
* Return a list of proposed code completions based on the specified
* location within the document that corresponds to the current cursor
* position within the text-editor control.
*
* @param textViewer
* @param documentPosition -
* the cursor location within the document
*
* an array of ICompletionProposals
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer textViewer, int documentPosition) {
setErrorMessage(null);
fTextViewer = textViewer;
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition);
Node node = (Node) treeNode;
while ((node != null) && (node.getNodeType() == Node.TEXT_NODE) && (node.getParentNode() != null)) {
node = node.getParentNode();
}
IDOMNode xmlnode = (IDOMNode) node;
ContentAssistRequest contentAssistRequest = null;
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
ITextRegion completionRegion = getCompletionRegion(documentPosition, node);
String matchString = getMatchString(sdRegion, completionRegion, documentPosition);
// Handle empty Documents
if (completionRegion == null) {
if (((treeNode == null) || (((Node) treeNode).getNodeType() == Node.DOCUMENT_NODE)) && (completionRegion == null) && ((xmlnode == null) || (xmlnode.getChildNodes() == null) || (xmlnode.getChildNodes().getLength() == 0))) {
IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(textViewer.getDocument());
try {
if (sModel != null) {
IDOMDocument docNode = ((IDOMModel) sModel).getDocument();
contentAssistRequest = newContentAssistRequest(docNode, docNode, sdRegion, completionRegion, documentPosition, 0, null);
addEmptyDocumentProposals(contentAssistRequest);
}
} finally {
if (sModel != null) {
sModel.releaseFromRead();
}
}
if (contentAssistRequest == null) {
// $NON-NLS-1$
Logger.logException(new IllegalStateException("problem getting model"));
return new ICompletionProposal[0];
}
return contentAssistRequest.getCompletionProposals();
}
// MASSIVE ERROR CONDITION
// $NON-NLS-1$
Logger.logException(new IllegalStateException("completion region was null"));
setErrorMessage(INTERNALERROR);
// $NON-NLS-1$
contentAssistRequest = newContentAssistRequest((Node) treeNode, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, "");
return contentAssistRequest.getCompletionProposals();
}
// catch documents where no region can be determined
if ((xmlnode.getNodeType() == Node.DOCUMENT_NODE) && ((completionRegion == null) || (xmlnode.getChildNodes() == null) || (xmlnode.getChildNodes().getLength() == 0))) {
contentAssistRequest = computeStartDocumentProposals(documentPosition, matchString, completionRegion, (IDOMNode) treeNode, xmlnode);
return contentAssistRequest.getCompletionProposals();
}
// compute normal proposals
contentAssistRequest = computeCompletionProposals(documentPosition, matchString, completionRegion, (IDOMNode) treeNode, xmlnode);
if (contentAssistRequest == null) {
// $NON-NLS-1$
contentAssistRequest = newContentAssistRequest((Node) treeNode, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, "");
if (Debug.displayWarnings) {
// $NON-NLS-2$//$NON-NLS-1$
System.out.println(UNKNOWN_CONTEXT + " " + completionRegion.getType() + "@" + documentPosition);
}
setErrorMessage(UNKNOWN_CONTEXT);
}
/*
* 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(UNKNOWN_CONTEXT);
}
return contentAssistRequest.getCompletionProposals();
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class DesignPageNavigationLocation method getSelection.
private ISelection getSelection() {
ISelection selection = null;
IStructuredModel model = null;
final ITextEditor editor = getTextEditorPart();
if (editor != null) {
try {
final IDocument document = getDocument(editor);
if (document instanceof IStructuredDocument) {
model = StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument) document);
if (model != null) {
final IndexedRegion region = model.getIndexedRegion(fPosition.offset);
if (region != null) {
selection = new StructuredSelection(region);
}
}
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
return selection;
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class RemoveBlockCommentActionXMLDelegate method processAction.
void processAction(IDocument document, ITextSelection textSelection) {
IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document);
if (model != null) {
try {
IndexedRegion selectionStartIndexedRegion = model.getIndexedRegion(textSelection.getOffset());
IndexedRegion selectionEndIndexedRegion = model.getIndexedRegion(textSelection.getOffset() + textSelection.getLength());
if ((selectionStartIndexedRegion == null) || (selectionEndIndexedRegion == null)) {
return;
}
int openCommentOffset = selectionStartIndexedRegion.getStartOffset();
int closeCommentOffset = selectionEndIndexedRegion.getEndOffset() - OPEN_COMMENT.length() - CLOSE_COMMENT.length();
model.beginRecording(this, XMLUIMessages.RemoveBlockComment_tooltip);
model.aboutToChangeModel();
try {
if (textSelection.getLength() == 0) {
if (selectionStartIndexedRegion instanceof CommentImpl) {
// $NON-NLS-1$
document.replace(openCommentOffset, OPEN_COMMENT.length(), "");
// $NON-NLS-1$
document.replace(closeCommentOffset, CLOSE_COMMENT.length(), "");
}
} else {
if (selectionStartIndexedRegion instanceof CommentImpl) {
// $NON-NLS-1$
document.replace(openCommentOffset, OPEN_COMMENT.length(), "");
}
if (selectionEndIndexedRegion instanceof CommentImpl) {
// $NON-NLS-1$
document.replace(closeCommentOffset, CLOSE_COMMENT.length(), "");
}
}
removeOpenCloseComments(document, openCommentOffset + OPEN_COMMENT.length(), closeCommentOffset - openCommentOffset - CLOSE_COMMENT.length());
} catch (BadLocationException e) {
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
} finally {
model.changedModel();
model.endRecording(this);
}
} finally {
model.releaseFromEdit();
}
}
}
Aggregations