use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class JSONFoldingStrategy method calcNewFoldPosition.
/**
* @see org.eclipse.wst.sse.ui.internal.projection.AbstractFoldingStrategy#calcNewFoldPosition(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)
*/
protected Position calcNewFoldPosition(IndexedRegion indexedRegion) {
Position retPos = null;
// only want to fold regions of the valid type and with a valid range
if (indexedRegion.getStartOffset() >= 0 && indexedRegion.getLength() >= 0) {
IJSONNode node = (IJSONNode) indexedRegion;
IStructuredDocumentRegion startRegion = node.getStartStructuredDocumentRegion();
IStructuredDocumentRegion endRegion = node.getEndStructuredDocumentRegion();
// don't fold it
if (startRegion != null && endRegion != null) {
if (startRegion.getEndOffset() == endRegion.getStartOffset())
return null;
if (endRegion.getEndOffset() >= startRegion.getStartOffset() && endRegion.getEndOffset() <= getDocument().getLength())
retPos = new JSONObjectFoldingPosition(startRegion, endRegion);
}
// else if(startRegion != null && indexedRegion instanceof
// CommentImpl) {
// retPos = new JSONCommentFoldingPosition(startRegion);
// }
}
return retPos;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class JSPContentAssistHelper method getResource.
/**
* Returns project request is in
*
* @param request
* @return {@link IResource} representing the project the given request was made in
*/
public static IResource getResource(ContentAssistRequest request) {
IResource resource = null;
String baselocation = null;
if (request != null) {
IStructuredDocumentRegion region = request.getDocumentRegion();
if (region != null) {
IDocument document = region.getParentDocument();
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model != null) {
baselocation = model.getBaseLocation();
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
}
if (baselocation != null) {
// copied from JSPTranslationAdapter#getJavaProject
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath filePath = new Path(baselocation);
IFile file = null;
if (filePath.segmentCount() > 1) {
file = root.getFile(filePath);
}
if (file != null) {
resource = file.getProject();
}
}
return resource;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class JSPELCompletionProposalComputer method computeCompletionProposals.
/**
* @see org.eclipse.jst.jsp.ui.internal.contentassist.JSPJavaCompletionProposalComputer#computeCompletionProposals(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
*/
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
ITextViewer viewer = context.getViewer();
int documentPosition = context.getInvocationOffset();
// get results from JSP completion processor
// 3 for the "get" at the beginning of the java proposal
List results = new ArrayList(computeJavaCompletionProposals(viewer, documentPosition, 3));
// get the function proposals for syntax like: ${ fn:| }
IStructuredDocumentRegion flat = ContentAssistUtils.getStructuredDocumentRegion(viewer, documentPosition);
if (flat != null) {
ITextRegion cursorRegion = flat.getRegionAtCharacterOffset(documentPosition);
String elText;
int startOffset;
// else can use flat region
if (cursorRegion instanceof ITextRegionContainer) {
ITextRegionContainer container = (ITextRegionContainer) cursorRegion;
cursorRegion = container.getRegionAtCharacterOffset(documentPosition);
elText = container.getText(cursorRegion);
startOffset = container.getStartOffset(cursorRegion);
} else {
elText = flat.getText(cursorRegion);
startOffset = flat.getStartOffset(cursorRegion);
}
// sanity check that we are actually in EL region
if (cursorRegion.getType() == DOMJSPRegionContexts.JSP_EL_CONTENT) {
String prefix = getPrefix(documentPosition - startOffset, elText);
if (null != prefix) {
List proposals = getFunctionProposals(prefix, viewer, documentPosition);
results.addAll(proposals);
}
}
}
return results;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class StructuredAutoEditStrategyJSP method smartInsertForEndTag.
private void smartInsertForEndTag(DocumentCommand command, IDocument document, IStructuredModel model) {
try {
if (command.text.equals("/") && (document.getLength() >= 1) && document.get(command.offset - 1, 1).equals("<") && HTMLUIPlugin.getDefault().getPreferenceStore().getBoolean(HTMLUIPreferenceNames.TYPING_COMPLETE_END_TAGS)) {
// $NON-NLS-1$ //$NON-NLS-2$
IDOMNode parentNode = (IDOMNode) ((IDOMNode) model.getIndexedRegion(command.offset - 1)).getParentNode();
if (isCommentNode(parentNode)) {
// loop and find non comment node parent
while ((parentNode != null) && isCommentNode(parentNode)) {
parentNode = (IDOMNode) parentNode.getParentNode();
}
}
if (!isDocumentNode(parentNode)) {
// only add end tag if one does not already exist or if
// add '/' does not create one already
IStructuredDocumentRegion endTagStructuredDocumentRegion = parentNode.getEndStructuredDocumentRegion();
IDOMNode ancestor = parentNode;
boolean smartInsertForEnd = false;
if (endTagStructuredDocumentRegion != null) {
// Look for ancestors by the same name that are missing end tags
while ((ancestor = (IDOMNode) ancestor.getParentNode()) != null) {
if (ancestor.getEndStructuredDocumentRegion() == null && parentNode.getNodeName().equals(ancestor.getNodeName())) {
smartInsertForEnd = true;
break;
}
}
}
if (endTagStructuredDocumentRegion == null || smartInsertForEnd) {
StringBuffer toAdd = new StringBuffer(parentNode.getNodeName());
if (toAdd.length() > 0) {
// $NON-NLS-1$
toAdd.append(">");
String suffix = toAdd.toString();
if ((document.getLength() < command.offset + suffix.length()) || (!suffix.equals(document.get(command.offset, suffix.length())))) {
command.text += suffix;
}
}
}
}
}
} catch (BadLocationException e) {
Logger.logException(e);
}
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class AutoImportProposal method getInsertPosition.
/**
* @param doc
* @param isXml
* @return position after <jsp:root> if xml, otherwise right before the document element
*/
private int getInsertPosition(IDocument doc, boolean isXml) {
int pos = 0;
IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
try {
if (sModel != null) {
if (sModel instanceof IDOMModel) {
IDOMDocument documentNode = ((IDOMModel) sModel).getDocument();
/*
* document element must be sole Element child of Document
* to remain valid
*/
Node targetElement = null;
if (isXml) {
targetElement = documentNode.getDocumentElement();
}
if (targetElement == null)
targetElement = getInsertNode(documentNode);
if (targetElement != null) {
IStructuredDocumentRegion sdRegion = ((IDOMNode) targetElement).getFirstStructuredDocumentRegion();
if (isXml) {
/*
* document Element must be sole Element child of
* Document to remain valid, so insert after
*/
pos = sdRegion.getEndOffset();
try {
while (pos < doc.getLength() && (doc.getChar(pos) == '\r' || doc.getChar(pos) == '\n')) {
pos++;
}
} catch (BadLocationException e) {
// not important, use pos as determined earlier
}
} else {
// insert before target element
pos = sdRegion.getStartOffset();
}
} else {
pos = 0;
}
}
}
} finally {
if (sModel != null)
sModel.releaseFromRead();
}
return pos;
}
Aggregations