use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument in project webtools.sourceediting by eclipse.
the class JSONModelImpl method newModel.
@Override
public void newModel(NewDocumentEvent structuredDocumentEvent) {
if (structuredDocumentEvent == null)
return;
IStructuredDocument structuredDocument = structuredDocumentEvent.getStructuredDocument();
if (structuredDocument == null)
return;
// this should not happen, but for the case
if (fStructuredDocument != null && fStructuredDocument != structuredDocument)
setStructuredDocument(structuredDocument);
internalSetNewDocument(structuredDocument);
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument in project webtools.sourceediting by eclipse.
the class JSPActionValidator method performValidation.
protected void performValidation(IFile f, IReporter reporter, IStructuredModel model, IRegion validateRegion) {
loadPreferences(f);
IStructuredDocument sDoc = model.getStructuredDocument();
fIsELIgnored = isElIgnored(f.getFullPath(), model);
// iterate all document regions
IStructuredDocumentRegion region = sDoc.getRegionAtCharacterOffset(validateRegion.getOffset());
while (region != null && !reporter.isCancelled() && (region.getStartOffset() <= (validateRegion.getOffset() + validateRegion.getLength()))) {
if (region.getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_NAME) {
// only checking directives
processDirective(reporter, f, model, region);
fTaglibPrefixes.clear();
} else if (region.getType() == DOMRegionContext.XML_TAG_NAME) {
// and jsp tags
String tagName = getStartTagName(region);
int colonPosition = tagName.indexOf(':');
if (colonPosition > -1) {
// get tag's prefix and check if it's really a jsp action
// tag
String prefix = tagName.substring(0, colonPosition);
if (getTaglibPrefixes(sDoc).contains(prefix))
processDirective(reporter, f, model, region);
}
}
region = region.getNext();
}
unloadPreferences();
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument in project webtools.sourceediting by eclipse.
the class TaglibModelQueryExtension method getAvailableElementContent.
/**
* @see org.eclipse.wst.xml.core.internal.contentmodel.modelquery.extension.ModelQueryExtension#getAvailableElementContent(org.w3c.dom.Element, java.lang.String, int)
*/
public CMNode[] getAvailableElementContent(Element parentElement, String namespace, int includeOptions) {
CMNode[] nodes = EMPTY_CMNODE_ARRAY;
ArrayList nodeList = new ArrayList();
// only returns anything if looking for child nodes
if (((includeOptions & ModelQuery.INCLUDE_CHILD_NODES) != 0) && parentElement instanceof IDOMElement) {
// get the trackers
IDOMElement elem = (IDOMElement) parentElement;
IStructuredDocument structDoc = elem.getModel().getStructuredDocument();
TLDCMDocumentManager manager = TaglibController.getTLDCMDocumentManager(structDoc);
if (manager != null) {
List trackers = new ArrayList(manager.getTaglibTrackers());
Set prefixes = new HashSet();
// for each tracker add each of its elements to the node list
for (int trackerIndex = 0; trackerIndex < trackers.size(); ++trackerIndex) {
TaglibTracker tracker = ((TaglibTracker) trackers.get(trackerIndex));
CMNamedNodeMap elements = tracker.getElements();
for (int elementIndex = 0; elementIndex < elements.getLength(); ++elementIndex) {
nodeList.add(elements.item(elementIndex));
}
prefixes.add(tracker.getPrefix());
}
String prefix = parentElement.getPrefix();
if (prefixes.contains(prefix)) {
Node parent = parentElement;
while ((parent = parent.getParentNode()) != null && parent.getNodeType() == Node.ELEMENT_NODE) {
prefix = parent.getPrefix();
if (prefix == null || !prefixes.contains(prefix)) {
ModelQuery query = ModelQueryUtil.getModelQuery(parentElement.getOwnerDocument());
if (query != null) {
CMElementDeclaration decl = query.getCMElementDeclaration((Element) parent);
if (decl != null && !fExtensions.contains(this)) {
fExtensions.push(this);
nodeList.addAll(query.getAvailableContent((Element) parent, decl, includeOptions));
fExtensions.pop();
}
}
break;
}
}
}
nodes = (CMNode[]) nodeList.toArray(new CMNode[nodeList.size()]);
}
}
return nodes;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument 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.IStructuredDocument 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);
}
}
}
Aggregations