use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class JSPJavaCompletionProposalComputer method isValidContext.
/**
* <p>Determines if the context is a valid one for JSP Java proposals.
* The default result is <code>true</code></p>
*
* @param context check this context to see if it is valid for JSP
* Java proposals
* @return <code>true</code> if the given context is a valid one for
* JSP Java proposals, <code>false</code> otherwise. <code>true</code>
* is the default response if a specific case for <code>false</code> is
* not found.
*/
private boolean isValidContext(CompletionProposalInvocationContext context) {
ITextViewer viewer = context.getViewer();
int documentPosition = context.getInvocationOffset();
String partitionType = getPartitionType(viewer, documentPosition);
if (partitionType == IJSPPartitions.JSP_CONTENT_JAVA)
return true;
IStructuredDocument structuredDocument = (IStructuredDocument) viewer.getDocument();
IStructuredDocumentRegion fn = structuredDocument.getRegionAtCharacterOffset(documentPosition);
IStructuredDocumentRegion sdRegion = ContentAssistUtils.getStructuredDocumentRegion(viewer, documentPosition);
// check for xml-jsp tags...
if (partitionType == IJSPPartitions.JSP_DIRECTIVE && fn != null) {
IStructuredDocumentRegion possibleXMLJSP = ((fn.getType() == DOMRegionContext.XML_CONTENT) && fn.getPrevious() != null) ? fn.getPrevious() : fn;
ITextRegionList regions = possibleXMLJSP.getRegions();
if (regions.size() > 1) {
// check bounds cases
ITextRegion xmlOpenOrClose = regions.get(0);
if (xmlOpenOrClose.getType() != DOMRegionContext.XML_TAG_OPEN && documentPosition != possibleXMLJSP.getStartOffset() && xmlOpenOrClose.getType() != DOMRegionContext.XML_END_TAG_OPEN && documentPosition <= possibleXMLJSP.getStartOffset()) {
// possible xml-jsp
ITextRegion nameRegion = regions.get(1);
String name = possibleXMLJSP.getText(nameRegion);
if (name.equals("jsp:scriptlet") || name.equals("jsp:expression") || name.equals("jsp:declaration")) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return true;
}
}
}
}
// check for XML-JSP in a <script> region
if (partitionType == IJSPPartitions.JSP_CONTENT_JAVASCRIPT || partitionType == IHTMLPartitions.SCRIPT) {
// fn should be block text
IStructuredDocumentRegion decodedSDRegion = decodeScriptBlock(fn.getFullText());
// decodedSDRegion.getEndOffset()));
if (decodedSDRegion != null) {
IStructuredDocumentRegion sdr = decodedSDRegion;
while (sdr != null) {
// sdr.getEndOffset()));
if (sdr.getType() == DOMJSPRegionContexts.JSP_CONTENT) {
if (documentPosition >= fn.getStartOffset() + sdr.getStartOffset() && documentPosition <= fn.getStartOffset() + sdr.getEndOffset()) {
return true;
}
} else if (sdr.getType() == DOMRegionContext.XML_TAG_NAME) {
if (documentPosition > fn.getStartOffset() + sdr.getStartOffset() && documentPosition < fn.getStartOffset() + sdr.getEndOffset()) {
return false;
} else if (documentPosition == fn.getStartOffset() + sdr.getEndOffset() && sdr.getNext() != null && sdr.getNext().getType() == DOMJSPRegionContexts.JSP_CONTENT) {
// <jsp:scriptlet>| blah </jsp:scriptlet>
return true;
} else if (documentPosition == fn.getStartOffset() + sdr.getStartOffset() && sdr.getPrevious() != null && sdr.getPrevious().getType() == DOMRegionContext.XML_TAG_NAME) {
return true;
}
}
sdr = sdr.getNext();
}
}
}
// check special JSP delimiter cases
if (fn != null && partitionType == IJSPPartitions.JSP_CONTENT_DELIMITER) {
IStructuredDocumentRegion fnDelim = fn;
// if it's a nested JSP region, need to get the correct
// StructuredDocumentRegion
// not sure why this check was there...
// if (fnDelim.getType() == XMLRegionContext.BLOCK_TEXT) {
Iterator blockRegions = fnDelim.getRegions().iterator();
ITextRegion temp = null;
ITextRegionContainer trc;
while (blockRegions.hasNext()) {
temp = (ITextRegion) blockRegions.next();
// we hit a nested
if (temp instanceof ITextRegionContainer) {
trc = (ITextRegionContainer) temp;
// it's in this region
if (documentPosition >= trc.getStartOffset() && documentPosition < trc.getEndOffset()) {
Iterator nestedJSPRegions = trc.getRegions().iterator();
while (nestedJSPRegions.hasNext()) {
temp = (ITextRegion) nestedJSPRegions.next();
if (XMLContentAssistUtilities.isJSPOpenDelimiter(temp.getType()) && documentPosition == trc.getStartOffset(temp)) {
// adapter
if (documentPosition > 0) {
partitionType = getPartitionType(viewer, documentPosition - 1);
break;
}
} else if (XMLContentAssistUtilities.isJSPCloseDelimiter(temp.getType()) && documentPosition == trc.getStartOffset(temp)) {
// JSP content assist
return true;
}
}
}
}
// }
}
// take care of XML-JSP delimter cases
if (XMLContentAssistUtilities.isXMLJSPDelimiter(fnDelim)) {
// since it's a delimiter, we know it's a ITextRegionContainer
ITextRegion firstRegion = fnDelim.getRegions().get(0);
if (fnDelim.getStartOffset() == documentPosition && (firstRegion.getType() == DOMRegionContext.XML_TAG_OPEN)) {
// |<jsp:scriptlet> </jsp:scriptlet>
// (pa) commented out so that we get regular behavior JSP
// macros etc...
// return getHTMLCompletionProposals(viewer,
// documentPosition);
} else if (fnDelim.getStartOffset() == documentPosition && (firstRegion.getType() == DOMRegionContext.XML_END_TAG_OPEN)) {
// adapter get the proposals
if (documentPosition > 0) {
String checkType = getPartitionType(viewer, documentPosition - 1);
if (checkType != IJSPPartitions.JSP_CONTENT_JAVASCRIPT) {
// check is failing for XML-JSP (region is not javascript...)
return true;
}
partitionType = IJSPPartitions.JSP_CONTENT_JAVASCRIPT;
}
} else if ((firstRegion.getType() == DOMRegionContext.XML_TAG_OPEN) && documentPosition >= fnDelim.getEndOffset()) {
// anything else inbetween
return true;
}
} else if (XMLContentAssistUtilities.isJSPDelimiter(fnDelim)) {
// the delimiter <%, <%=, <%!, ...
if (XMLContentAssistUtilities.isJSPCloseDelimiter(fnDelim)) {
if (documentPosition == fnDelim.getStartOffset()) {
// JAVASCRIPT adapter get the proposals
if (documentPosition > 0) {
String checkType = getPartitionType(viewer, documentPosition - 1);
if (checkType != IJSPPartitions.JSP_CONTENT_JAVASCRIPT) {
return true;
}
partitionType = IJSPPartitions.JSP_CONTENT_JAVASCRIPT;
}
}
} else if (XMLContentAssistUtilities.isJSPOpenDelimiter(fnDelim)) {
// use embedded HTML results
if (documentPosition == fnDelim.getEndOffset()) {
// it's at the EOF <%|
return true;
}
}
}
}
// <!-- <% |%> -->
if (fn != null && (fn.getType() == DOMRegionContext.XML_CDATA_TEXT || fn.getType() == DOMRegionContext.XML_COMMENT_TEXT)) {
if (fn instanceof ITextRegionContainer) {
Object[] cdataRegions = fn.getRegions().toArray();
ITextRegion r = null;
ITextRegion jspRegion = null;
for (int i = 0; i < cdataRegions.length; i++) {
r = (ITextRegion) cdataRegions[i];
if (r instanceof ITextRegionContainer) {
// CDATA embedded container, or comment container
Object[] jspRegions = ((ITextRegionContainer) r).getRegions().toArray();
for (int j = 0; j < jspRegions.length; j++) {
jspRegion = (ITextRegion) jspRegions[j];
if (jspRegion.getType() == DOMJSPRegionContexts.JSP_CLOSE) {
if (sdRegion.getStartOffset(jspRegion) == documentPosition) {
return true;
}
}
}
}
}
}
}
// check if it's in an attribute value, if so, don't add CDATA
// proposal
ITextRegion attrContainer = (fn != null) ? fn.getRegionAtCharacterOffset(documentPosition) : null;
if (attrContainer != null && attrContainer instanceof ITextRegionContainer) {
if (attrContainer.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
// test location of the cursor
// return null if it's in the middle of an open/close delimiter
Iterator attrRegions = ((ITextRegionContainer) attrContainer).getRegions().iterator();
ITextRegion testRegion = null;
while (attrRegions.hasNext()) {
testRegion = (ITextRegion) attrRegions.next();
// need to check for other valid attribute regions
if (XMLContentAssistUtilities.isJSPOpenDelimiter(testRegion.getType())) {
if (!(((ITextRegionContainer) attrContainer).getEndOffset(testRegion) <= documentPosition))
return false;
} else if (XMLContentAssistUtilities.isJSPCloseDelimiter(testRegion.getType())) {
if (!(((ITextRegionContainer) attrContainer).getStartOffset(testRegion) >= documentPosition))
return false;
}
}
// TODO: handle non-Java code such as nested tags
if (testRegion.getType().equals(DOMJSPRegionContexts.JSP_CONTENT)) {
return true;
}
return false;
}
}
return true;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class StructuredAutoEditStrategyJSPJava method customizeDocumentCommand.
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
if (!supportsSmartInsert(document)) {
return;
}
IStructuredModel model = null;
try {
// Auto edit for JSP Comments
if ("-".equals(command.text) && isPreferenceEnabled(JSPUIPreferenceNames.TYPING_COMPLETE_COMMENTS)) {
// $NON-NLS-1$
if (prefixedWith(document, command.offset, "<%-")) {
// $NON-NLS-1$
model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model != null) {
IDOMNode node = (IDOMNode) model.getIndexedRegion(command.offset);
IDOMNode parent = (node != null) ? (IDOMNode) node.getParentNode() : null;
// Parent is the scriptlet tag
if (parent != null && JSP11Namespace.ElementName.SCRIPTLET.equals(parent.getNodeName()) && !parent.getSource().endsWith("--%>")) {
// $NON-NLS-1$
IStructuredDocumentRegion end = parent.getEndStructuredDocumentRegion();
if (end != null) {
try {
// $NON-NLS-1$
document.replace(end.getStartOffset(), 0, "--");
} catch (BadLocationException e) {
Logger.logException(e);
}
}
}
}
}
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class FormattingStrategyJSDT method format.
/*
* @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#format()
*/
public void format() {
super.format();
final IStructuredDocument document = (IStructuredDocument) fDocuments.removeFirst();
final TypedPosition partition = (TypedPosition) fPartitions.removeFirst();
if (document != null) {
// calculate the indent of the leading <script> tag because we need to add that indent level to the JS indent level
IStructuredDocumentRegion scriptTagStartRegion = document.getRegionAtCharacterOffset(partition.offset - 1);
// $NON-NLS-1$
String scriptRegionIndent = "";
if (scriptTagStartRegion != null) {
try {
int scriptRegionIndentLevel = getIndentOfLine(document, document.getLineOfOffset(scriptTagStartRegion.getStartOffset())).length();
scriptRegionIndent = getIndentationString(getPreferences(), scriptRegionIndentLevel);
this.startIndentLevel += scriptRegionIndentLevel;
} catch (BadLocationException e) {
// $NON-NLS-1$
Logger.logException("Could not calculate starting indent of the script region, using 0", e);
}
}
String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
try {
// get the JS text from the document (not translated)
String jsTextNotTranslated = document.get(partition.getOffset(), partition.getLength());
String originalText = jsTextNotTranslated;
// deal with getting the JS text and unwrapping it from any <!-- //--> statements
String preText = "";
String postText = lineDelim + scriptRegionIndent;
// find and remove start comment tag if it's there
// $NON-NLS-1$
Pattern startPattern = Pattern.compile("(\\A(\\s*<!--.*(" + lineDelim + ")?))");
Matcher matcher = startPattern.matcher(jsTextNotTranslated);
if (matcher.find()) {
preText = lineDelim + scriptRegionIndent + matcher.group().trim();
// $NON-NLS-1$
jsTextNotTranslated = matcher.replaceFirst("");
}
// find and remove end comment tag if it's there
matcher = END_PATTERN.matcher(jsTextNotTranslated);
if (matcher.find()) {
// $NON-NLS-1$
jsTextNotTranslated = matcher.replaceFirst("");
postText = lineDelim + scriptRegionIndent + matcher.group().trim() + postText;
}
/*
* replace the text in the document with the non-translated JS
* text but without HTML leading and trailing comments
*/
int scriptLength = jsTextNotTranslated.length();
TextEdit replaceEdit = null;
if (scriptLength != originalText.length()) {
replaceEdit = new ReplaceEdit(partition.getOffset(), partition.getLength(), jsTextNotTranslated);
replaceEdit.apply(document);
}
// translate the web page without the script "wrapping"
IJsTranslation translation = getTranslation(document);
String jsTextTranslated = translation.getJsText();
/*
* Set a default replace text that is the original contents
* with a new line and proper indentation in front
*/
String replaceText = lineDelim + getIndentationString(getPreferences(), startIndentLevel) + jsTextNotTranslated;
int javaScriptOffset = ((JsTranslation) translation).getJavaScriptOffset(partition.getOffset());
// known range, proceed
if (javaScriptOffset >= 0) {
// format the translated text
TextEdit edit = CodeFormatterUtil.format2(CodeFormatter.K_JAVASCRIPT_UNIT, jsTextTranslated, javaScriptOffset, scriptLength, startIndentLevel, lineDelim, getPreferences());
IDocument jsDoc = new Document(jsTextTranslated);
if (edit != null) {
/*
* Put the original (possibly not JS) text back into the doc
* to which we're applying the edit
*/
if (translation instanceof JsTranslation) {
IJsTranslator translator = ((JsTranslation) translation).getTranslator();
if (translator instanceof JsTranslator) {
Region[] regions = ((JsTranslator) translator).getGeneratedRanges();
Arrays.sort(regions, new Comparator() {
public int compare(Object o1, Object o2) {
return ((IRegion) o1).getOffset() - ((IRegion) o2).getOffset();
}
});
/*
* for each web page range representing content needing replacements, replace it with the
* original web page's text
*/
for (int r = 0; r < regions.length; ++r) {
int javascriptOffset = ((JsTranslation) translation).getJavaScriptOffset(regions[r].getOffset());
if (javascriptOffset > 0) {
jsDoc.replace(javascriptOffset, regions[r].getLength(), document.get(regions[r].getOffset(), regions[r].getLength()));
}
}
}
}
edit.apply(jsDoc);
replaceText = lineDelim + getIndentationString(getPreferences(), startIndentLevel) + (jsDoc.get(edit.getOffset(), edit.getLength())).trim();
} else {
/*
* Revert changes (it may still appear dirty, though,
* because of the above edits having been applied)
*/
replaceEdit = new ReplaceEdit(partition.getOffset(), scriptLength, originalText);
replaceEdit.apply(document);
return;
}
}
// apply edit to html doc using the formated translated text and the possible leading and trailing html comments
replaceText = preText + replaceText + postText;
replaceEdit = new ReplaceEdit(partition.getOffset(), scriptLength, replaceText);
replaceEdit.apply(document);
} 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 DTDRegionParser method getDocumentRegions.
public IStructuredDocumentRegion getDocumentRegions() {
if (cachedNode != null) {
return cachedNode;
}
List regions = getRegions();
IStructuredDocumentRegion headNode = createNodeChain(regions);
cachedNode = headNode;
return headNode;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class DTDRegionParser method addNewNodes.
private IStructuredDocumentRegion addNewNodes(IStructuredDocumentRegion lastNode, Vector regions) {
IStructuredDocumentRegion leadingSpaceNode = null;
IStructuredDocumentRegion contentNode = null;
IStructuredDocumentRegion trailingSpaceNode = null;
LinkedList nodeSeeds = new LinkedList();
int nRegions = regions.size();
int leadingSpaceEnd = -1;
int trailingSpaceBegin = nRegions;
// find leading space
nodeSeeds.clear();
for (int i = 0; i < nRegions; i++) {
ITextRegion region = (ITextRegion) regions.get(i);
String type = region.getType();
if (isBlankRegion(type)) {
leadingSpaceEnd = i;
nodeSeeds.addLast(region);
} else {
break;
}
}
if (!nodeSeeds.isEmpty()) {
leadingSpaceNode = createNode(nodeSeeds);
if (lastNode != null) {
lastNode.setNext(leadingSpaceNode);
leadingSpaceNode.setPrevious(lastNode);
}
lastNode = leadingSpaceNode;
}
// find trailing space
if (leadingSpaceEnd < nRegions - 1) {
nodeSeeds.clear();
for (int i = nRegions - 1; 0 <= i; i--) {
ITextRegion region = (ITextRegion) regions.get(i);
String type = ((ITextRegion) regions.get(i)).getType();
if (isBlankRegion(type)) {
trailingSpaceBegin = i;
nodeSeeds.addFirst(region);
} else {
break;
}
}
if (!nodeSeeds.isEmpty()) {
trailingSpaceNode = createNode(nodeSeeds);
}
nodeSeeds.clear();
for (int i = leadingSpaceEnd + 1; i < trailingSpaceBegin; i++) {
nodeSeeds.addLast(regions.get(i));
}
if (!nodeSeeds.isEmpty()) {
contentNode = createNode(nodeSeeds);
if (lastNode != null) {
lastNode.setNext(contentNode);
contentNode.setPrevious(lastNode);
}
lastNode = contentNode;
}
if (trailingSpaceNode != null) {
lastNode.setNext(trailingSpaceNode);
trailingSpaceNode.setPrevious(lastNode);
lastNode = trailingSpaceNode;
}
}
return lastNode;
}
Aggregations