use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer in project webtools.sourceediting by eclipse.
the class JSPJavaContentAssistProcessor method computeCompletionProposals.
/**
* 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 documentPosition
* a location within the document
* @return an array of code-assist items
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentPosition) {
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(viewer, documentPosition);
// get results from JSP completion processor
fJspCompletionProcessor = getJspCompletionProcessor();
ICompletionProposal[] results = fJspCompletionProcessor.computeCompletionProposals(viewer, documentPosition);
fErrorMessage = fJspCompletionProcessor.getErrorMessage();
if (results.length == 0 && (fErrorMessage == null || fErrorMessage.length() == 0)) {
fErrorMessage = UNKNOWN_CONTEXT;
}
IDOMNode xNode = null;
IStructuredDocumentRegion flat = null;
if (treeNode instanceof IDOMNode) {
xNode = (IDOMNode) treeNode;
flat = xNode.getFirstStructuredDocumentRegion();
if (flat != null && flat.getType() == DOMJSPRegionContexts.JSP_CONTENT) {
flat = flat.getPrevious();
}
}
// this is in case it's a <%@, it will be a region container...
ITextRegion openRegion = null;
if (flat != null && flat instanceof ITextRegionContainer) {
ITextRegionList v = ((ITextRegionContainer) flat).getRegions();
if (v.size() > 0)
openRegion = v.get(0);
}
// ADD CDATA PROPOSAL IF IT'S AN XML-JSP TAG
if (flat != null && flat.getType() != DOMJSPRegionContexts.JSP_SCRIPTLET_OPEN && flat.getType() != DOMJSPRegionContexts.JSP_DECLARATION_OPEN && flat.getType() != DOMJSPRegionContexts.JSP_EXPRESSION_OPEN && flat.getType() != DOMRegionContext.BLOCK_TEXT && (openRegion != null && openRegion.getType() != DOMJSPRegionContexts.JSP_DIRECTIVE_OPEN) && !inAttributeRegion(flat, documentPosition)) {
// determine if cursor is before or after selected range
int adjustedDocPosition = documentPosition;
int realCaretPosition = viewer.getTextWidget().getCaretOffset();
int selectionLength = viewer.getSelectedRange().y;
if (documentPosition > realCaretPosition) {
adjustedDocPosition -= selectionLength;
}
CustomCompletionProposal cdataProposal = createCDATAProposal(adjustedDocPosition, selectionLength);
ICompletionProposal[] newResults = new ICompletionProposal[results.length + 1];
System.arraycopy(results, 0, newResults, 0, results.length);
newResults[results.length] = cdataProposal;
results = newResults;
}
return results;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer in project webtools.sourceediting by eclipse.
the class LibraryTagsCompletionProposalComputer method addAttributeValueProposals.
/**
* @see org.eclipse.wst.xml.ui.internal.contentassist.DefaultXMLCompletionProposalComputer#addAttributeValueProposals(org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest, org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext)
*/
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
if (!this.isXHTML) {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
ModelQuery mq = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
if (mq != null) {
CMDocument doc = mq.getCorrespondingCMDocument(node);
// this shouldn't have to have the prefix coded in
if (doc instanceof JSPCMDocument || doc instanceof CMNodeWrapper || node.getNodeName().startsWith("jsp:")) {
// $NON-NLS-1$
return;
}
}
// Find the attribute name for which this position should have a value
IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
ITextRegionList openRegions = open.getRegions();
int i = openRegions.indexOf(contentAssistRequest.getRegion());
if (i < 0) {
return;
}
ITextRegion nameRegion = null;
while (i >= 0) {
nameRegion = openRegions.get(i--);
if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
break;
}
}
// on an empty value, add all the JSP and taglib tags
CMElementDeclaration elementDecl = AbstractXMLModelQueryCompletionProposalComputer.getCMElementDeclaration(node);
if (nameRegion != null && elementDecl != null) {
String attributeName = open.getText(nameRegion);
if (attributeName != null) {
Node parent = contentAssistRequest.getParent();
// ignore start quote in match string
String matchString = contentAssistRequest.getMatchString().trim();
if (matchString.startsWith("'") || matchString.startsWith("\"")) {
// $NON-NLS-1$ //$NON-NLS-2$
matchString = matchString.substring(1);
}
// get all the proposals
List additionalElements = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ALL);
Iterator nodeIterator = additionalElements.iterator();
// check each suggestion
while (nodeIterator.hasNext()) {
CMNode additionalElementDecl = (CMNode) nodeIterator.next();
if (additionalElementDecl != null && additionalElementDecl instanceof CMElementDeclaration && validModelQueryNode(additionalElementDecl)) {
CMElementDeclaration ed = (CMElementDeclaration) additionalElementDecl;
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
StringBuffer sb = new StringBuffer();
getContentGenerator().generateTag(parent, ed, sb);
String proposedText = sb.toString();
// filter out any proposals that dont match matchString
if (beginsWith(proposedText, matchString)) {
// wrap with ' because JSP attributes are warped with "
// $NON-NLS-1$
proposedText = "'" + proposedText;
// don't want to risk injecting an extra
if (!(contentAssistRequest.getRegion() instanceof ITextRegionContainer)) {
// $NON-NLS-1$
proposedText += "'";
}
// get the image
Image image = CMImageUtil.getImage(elementDecl);
if (image == null) {
image = this.getGenericTagImage();
}
// create the proposal
int cursorAdjustment = getCursorPositionForProposedText(proposedText);
String proposedInfo = AbstractXMLModelQueryCompletionProposalComputer.getAdditionalInfo(AbstractXMLModelQueryCompletionProposalComputer.getCMElementDeclaration(parent), elementDecl);
String tagname = getContentGenerator().getRequiredName(node, ed);
CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, tagname, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
}
}
}
}
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer in project webtools.sourceediting by eclipse.
the class XMLSourceParser method parseNodes.
protected IStructuredDocumentRegion parseNodes() {
// regions are initially reported as complete offsets within the
// scanned input
// they are adjusted here to be indexes from the currentNode's start
// offset
IStructuredDocumentRegion headNode = null;
IStructuredDocumentRegion lastNode = null;
ITextRegion region = null;
IStructuredDocumentRegion currentNode = null;
String type = null;
while ((region = getNextRegion()) != null) {
type = region.getType();
// of them
if (type == DOMRegionContext.BLOCK_TEXT) {
if (currentNode != null && currentNode.getLastRegion().getType() == DOMRegionContext.BLOCK_TEXT) {
// multiple block texts indicated embedded containers; no
// new IStructuredDocumentRegion
currentNode.addRegion(region);
currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
region.adjustStart(-currentNode.getStart());
// DW 4/16/2003 regions no longer have parents
// region.setParent(currentNode);
} else {
// not continuing a IStructuredDocumentRegion
if (currentNode != null) {
// terminated
if (!currentNode.isEnded()) {
currentNode.setLength(region.getStart() - currentNode.getStart());
// fCurrentNode.setTextLength(region.getStart() -
// fCurrentNode.getStart());
}
lastNode = currentNode;
}
fireNodeParsed(currentNode);
currentNode = createStructuredDocumentRegion(type);
if (lastNode != null) {
lastNode.setNext(currentNode);
}
currentNode.setPrevious(lastNode);
currentNode.setStart(region.getStart());
currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
currentNode.setEnded(true);
region.adjustStart(-currentNode.getStart());
currentNode.addRegion(region);
// DW 4/16/2003 regions no longer have parents
// region.setParent(currentNode);
}
} else // the following contexts OPEN new StructuredDocumentRegions
if ((currentNode != null && currentNode.isEnded()) || (type == DOMRegionContext.XML_CONTENT) || (type == DOMRegionContext.XML_CHAR_REFERENCE) || (type == DOMRegionContext.XML_ENTITY_REFERENCE) || (type == DOMRegionContext.XML_PI_OPEN) || (type == DOMRegionContext.XML_TAG_OPEN) || (type == DOMRegionContext.XML_END_TAG_OPEN) || (type == DOMRegionContext.XML_COMMENT_OPEN) || (type == DOMRegionContext.XML_CDATA_OPEN) || (type == DOMRegionContext.XML_DECLARATION_OPEN)) {
if (currentNode != null) {
// ensure that any existing node is at least terminated
if (!currentNode.isEnded()) {
currentNode.setLength(region.getStart() - currentNode.getStart());
// fCurrentNode.setTextLength(region.getStart() -
// fCurrentNode.getStart());
}
lastNode = currentNode;
}
fireNodeParsed(currentNode);
currentNode = createStructuredDocumentRegion(type);
if (lastNode != null) {
lastNode.setNext(currentNode);
}
currentNode.setPrevious(lastNode);
currentNode.setStart(region.getStart());
currentNode.addRegion(region);
currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
region.adjustStart(-currentNode.getStart());
// DW 4/16/2003 regions no longer have parents
// region.setParent(currentNode);
} else // StructuredDocumentRegions; just add to them
if ((type == DOMRegionContext.XML_TAG_NAME) || (type == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) || (type == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) || (type == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) || (type == DOMRegionContext.XML_COMMENT_TEXT) || (type == DOMRegionContext.XML_PI_CONTENT) || (type == DOMRegionContext.XML_DOCTYPE_INTERNAL_SUBSET)) {
currentNode.addRegion(region);
currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
region.adjustStart(-currentNode.getStart());
// DW 4/16/2003 regions no longer have parents
// region.setParent(currentNode);
} else // cleanly
if ((type == DOMRegionContext.XML_PI_CLOSE) || (type == DOMRegionContext.XML_TAG_CLOSE) || (type == DOMRegionContext.XML_EMPTY_TAG_CLOSE) || (type == DOMRegionContext.XML_COMMENT_CLOSE) || (type == DOMRegionContext.XML_DECLARATION_CLOSE) || (type == DOMRegionContext.XML_CDATA_CLOSE)) {
currentNode.setEnded(true);
currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
currentNode.addRegion(region);
region.adjustStart(-currentNode.getStart());
// DW 4/16/2003 regions no longer have parents
// region.setParent(currentNode);
} else // this is extremely rare, but valid
if (type == DOMRegionContext.WHITE_SPACE) {
ITextRegion lastRegion = currentNode.getLastRegion();
// pack the embedded container with this region
if (lastRegion instanceof ITextRegionContainer) {
ITextRegionContainer container = (ITextRegionContainer) lastRegion;
container.getRegions().add(region);
// containers must have parent set ...
// setting for EACH subregion is redundent, but not sure
// where else to do, so will do here for now.
container.setParent(currentNode);
// DW 4/16/2003 regions no longer have parents
// region.setParent(container);
region.adjustStart(container.getLength() - region.getStart());
}
currentNode.getLastRegion().adjustLength(region.getLength());
currentNode.adjustLength(region.getLength());
} else if (type == DOMRegionContext.UNDEFINED && currentNode != null) {
// combine with previous if also undefined
if (currentNode.getLastRegion() != null && currentNode.getLastRegion().getType() == DOMRegionContext.UNDEFINED) {
currentNode.getLastRegion().adjustLength(region.getLength());
currentNode.adjustLength(region.getLength());
} else // previous wasn't undefined
{
currentNode.addRegion(region);
currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
region.adjustStart(-currentNode.getStart());
}
} else {
// ensure that a node exists
if (currentNode == null) {
currentNode = createStructuredDocumentRegion(type);
currentNode.setStart(region.getStart());
}
currentNode.addRegion(region);
currentNode.setLength(region.getStart() + region.getLength() - currentNode.getStart());
region.adjustStart(-currentNode.getStart());
// region.setParent(currentNode);
if (Debug.debugTokenizer)
// $NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
System.out.println(getClass().getName() + " found region of not specifically handled type " + region.getType() + " @ " + region.getStart() + "[" + region.getLength() + "]");
// $NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
}
// ensures that they open StructuredDocumentRegions the same way
if ((type == DOMRegionContext.XML_CONTENT) || (type == DOMRegionContext.XML_CHAR_REFERENCE) || (type == DOMRegionContext.XML_ENTITY_REFERENCE)) {
currentNode.setEnded(true);
}
if (headNode == null && currentNode != null) {
headNode = currentNode;
}
}
if (currentNode != null) {
fireNodeParsed(currentNode);
currentNode.setPrevious(lastNode);
}
// fStringInput = null;
primReset();
return headNode;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer in project webtools.sourceediting by eclipse.
the class AbstractJSONCompletionProposalComputer method computeObjectKeyProposals.
private ContentAssistRequest computeObjectKeyProposals(String matchString, ITextRegion completionRegion, IJSONNode nodeAtOffset, IJSONNode node, CompletionProposalInvocationContext context) {
int documentPosition = context.getInvocationOffset();
ContentAssistRequest contentAssistRequest = null;
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
int replaceLength = 0;
int begin = documentPosition;
if (completionRegion.getType() == JSONRegionContexts.JSON_OBJECT_KEY || completionRegion.getType() == JSONRegionContexts.JSON_UNKNOWN) {
replaceLength = completionRegion.getTextLength();
// value region not the entire container
if (completionRegion instanceof ITextRegionContainer) {
ITextRegion openRegion = ((ITextRegionContainer) completionRegion).getFirstRegion();
ITextRegion closeRegion = ((ITextRegionContainer) completionRegion).getLastRegion();
if (openRegion.getType() != closeRegion.getType()) {
replaceLength = openRegion.getTextLength();
}
}
begin = sdRegion.getStartOffset(completionRegion);
}
if (isPairValue(context, nodeAtOffset)) {
IJSONPair pair = (IJSONPair) nodeAtOffset;
IJSONValue value = pair.getValue();
if (value != null) {
try {
begin = value.getStartOffset();
String valueText = getNodeText(value);
valueText = valueText.trim();
replaceLength = valueText.length();
if (valueText.startsWith(QUOTE)) {
begin = begin + 1;
replaceLength = replaceLength - 1;
}
if (valueText.endsWith(QUOTE)) {
replaceLength = replaceLength - 1;
}
} catch (BadLocationException e) {
// ignore
}
}
} else if (nodeAtOffset instanceof IJSONPair) {
IJSONPair pair = (IJSONPair) nodeAtOffset;
try {
begin = pair.getStartOffset();
String text = getNodeText(pair);
text = text.trim();
replaceLength = pair.getName().length();
if (text.startsWith(QUOTE)) {
begin = begin + 1;
}
} catch (BadLocationException e) {
// ignore
}
}
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node.getParentNode(), sdRegion, completionRegion, begin, replaceLength, matchString);
addObjectKeyProposals(contentAssistRequest, context);
return contentAssistRequest;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer in project webtools.sourceediting by eclipse.
the class ScanningTests method viewableTokenize.
public static String viewableTokenize(String stringdata, boolean useXML) {
java.util.List l = parse(stringdata, useXML);
String s = "";
for (int i = 0; i < l.size(); i++) {
IStructuredDocumentRegion node = (IStructuredDocumentRegion) l.get(i);
try {
s += StringUtils.escape(node.toString()) + "\n";
} catch (Exception e) {
s += "[" + node.getStart() + ", " + node.getEnd() + "] (UNPRINTABLE " + e + ")";
}
ITextRegionList m = node.getRegions();
for (int j = 0; j < m.size(); j++) if (m.get(j) instanceof ITextRegionContainer) {
s = s + "\t" + StringUtils.escape(m.get(j).toString()) + "\n";
ITextRegionList n = ((ITextRegionContainer) m.get(j)).getRegions();
for (int k = 0; k < n.size(); k++) s = s + "\t\t" + StringUtils.escape(n.get(k).toString()) + "\n";
} else
s = s + "\t" + StringUtils.escape(m.get(j).toString()) + "\n";
}
return s;
}
Aggregations