use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class CSSStructuredDocumentReParser method checkForCrossStructuredDocumentRegionSyntax.
/**
*/
// public StructuredDocumentEvent
// checkForCrossStructuredDocumentRegionBoundryCases2() {
// StructuredDocumentEvent result = specialPositionUpdate(fStart, fStart +
// fLengthToReplace - 1);
// if (result == null) {
// result = checkInsideBrace();
// }
// return result;
// }
protected StructuredDocumentEvent checkForCrossStructuredDocumentRegionSyntax() {
int checkStart = fStart;
int checkEnd = fStart + fLengthToReplace - 1;
IStructuredDocumentRegion endRegion = fStructuredDocument.getRegionAtCharacterOffset(checkEnd);
if (endRegion != null) {
checkEnd = endRegion.getEndOffset();
}
ReparseRange range = new ReparseRange(checkStart, checkEnd);
range.expand(getUpdateRangeForDelimiter(range.getStart(), range.getEnd()));
range.expand(getUpdateRangeForUnknownRegion(range.getStart(), range.getEnd()));
range.expand(getUpdateRangeForQuotes(range.getStart(), range.getEnd()));
range.expand(getUpdateRangeForComments(range.getStart(), range.getEnd()));
range.expand(getUpdateRangeForBraces(range.getStart(), range.getEnd()));
StructuredDocumentEvent result;
result = checkInsideBrace(range.getStart(), range.getEnd());
if (result == null) {
result = reparse(range.getStart(), range.getEnd());
}
return result;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class JsTranslator method translateJSNode.
/* (non-Javadoc)
* @see org.eclipse.wst.jsdt.web.core.javascript.IJsTranslator#translateJSNode(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)
*/
public void translateJSNode(IStructuredDocumentRegion container) {
if (container == null)
return;
ITextRegionCollection containerRegion = container;
Iterator regions = containerRegion.getRegions().iterator();
ITextRegion region = null;
char[] spaces = Util.getPad(container.getStartOffset() - scriptOffset);
for (int i = 0; i < spaces.length; i++) {
try {
char c = fStructuredDocument.getChar(scriptOffset + i);
if (c == '\n' || c == '\r' || c == '\t')
spaces[i] = c;
} catch (BadLocationException e) {
Logger.logException(e);
}
}
fScriptText.append(spaces);
scriptOffset = container.getStartOffset();
if (container.getType() != DOMRegionContext.BLOCK_TEXT && container.getType() != DOMRegionContext.XML_CDATA_TEXT) {
// add place holder in position map (for formatting)
appendAndTrack("", scriptOffset);
return;
}
while (regions.hasNext() && !isCanceled()) {
region = (ITextRegion) regions.next();
String type = region.getType();
// content assist was not showing up in JSP inside a javascript
// region
// System.out.println("Region text: " + container.getText().substring(region.getStart(), region.getEnd()));
boolean isContainerRegion = region instanceof ITextRegionContainer;
/* make sure its not a sub container region, probably JSP */
if (type == DOMRegionContext.BLOCK_TEXT) {
int scriptStartOffset = container.getStartOffset(region);
int scriptTextLength = container.getLength();
String regionText = container.getFullText(region);
// regionText = StringUtils.replace(regionText, CDATA_START, CDATA_START_PAD);
// regionText = StringUtils.replace(regionText, CDATA_END, CDATA_END_PAD);
int regionLength = region.getLength();
spaces = Util.getPad(scriptStartOffset - scriptOffset);
for (int i = 0; i < spaces.length; i++) {
try {
char c = fStructuredDocument.getChar(scriptOffset + i);
if (c == '\n' || c == '\r' || c == '\t')
spaces[i] = c;
} catch (BadLocationException e) {
Logger.logException(e);
}
}
fScriptText.append(spaces);
// skip over XML/HTML comment starts
if (regionText.indexOf(XML_COMMENT_START) >= 0) {
int index = regionText.indexOf(XML_COMMENT_START);
boolean replaceCommentStart = true;
for (int i = 0; i < index; i++) {
/*
* replace the comment start in the translation when
* it's preceded only by white space or '/'
*/
replaceCommentStart = replaceCommentStart && (Character.isWhitespace(regionText.charAt(i)) || '/' == regionText.charAt(i));
}
if (replaceCommentStart) {
IRegion line;
int end;
int length;
try {
/*
* try to find where the line with the comment
* ends (it is the end of what we'll replace)
*/
line = container.getParentDocument().getLineInformationOfOffset(index + scriptStartOffset);
end = line.getOffset() + line.getLength() - scriptStartOffset;
if (end > regionText.length()) {
end = regionText.length();
}
length = end - index;
} catch (BadLocationException e) {
// $NON-NLS-1$
Logger.logException("Could not get web page's comment line information", e);
end = index + XML_COMMENT_START.length();
length = XML_COMMENT_START.length();
}
scriptStartOffset += end;
regionText = regionText.substring(end);
appendAndTrack(new String(Util.getPad(end)), fScriptText.length());
}
}
/*
* Fix for
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=284774
* end of last valid JS source, start of next content to
* skip
*/
// last offset of valid JS source, after which there's server-side stuff
int validJSend = 0;
// start of next valid JS source, last offset of content that was skipped
int validJSstart = 0;
Matcher matcher = fClientSideTagPattern.matcher(regionText);
// note the start of a HTML tag if one's present
int clientMatchStart = matcher.find() ? matcher.start() : -1;
StringBuffer generatedContent = new StringBuffer();
int serverSideStart = -1;
int serverSideDelimiter = 0;
// find any instance of server code blocks in the region text
for (int i = 0; i < fServerSideDelimiters.length; i++) {
int index = regionText.indexOf(fServerSideDelimiters[i][0]);
if (serverSideStart < 0) {
serverSideStart = index;
serverSideDelimiter = i;
} else if (index >= 0) {
serverSideStart = Math.min(serverSideStart, index);
if (serverSideStart == index) {
serverSideDelimiter = i;
}
}
}
// contains something other than pure JavaScript
while (serverSideStart > -1 || clientMatchStart > -1) {
// $NON-NLS-1$
validJSend = validJSstart;
boolean biasClient = false;
boolean biasServer = false;
// update the start of content to skip
if (clientMatchStart > -1 && serverSideStart > -1) {
validJSend = Math.min(clientMatchStart, serverSideStart);
biasClient = validJSend == clientMatchStart;
biasServer = validJSend == serverSideStart;
} else if (clientMatchStart > -1 && serverSideStart < 0) {
validJSend = clientMatchStart;
biasClient = true;
} else if (clientMatchStart < 0 && serverSideStart > -1) {
validJSend = serverSideStart;
biasServer = true;
}
// append if there's something we want to include
if (-1 < validJSstart && -1 < validJSend) {
// append what we want to include
appendAndTrack(regionText.substring(validJSstart, validJSend), scriptStartOffset + validJSstart);
// change the skipped content to a valid variable name and append it as a placeholder
int startOffset = scriptStartOffset + validJSend;
String serverEnd = fServerSideDelimiters[serverSideDelimiter][1];
int serverSideEnd = (regionLength > validJSend + serverEnd.length()) ? regionText.indexOf(serverEnd, validJSend + fServerSideDelimiters[serverSideDelimiter][1].length()) : -1;
if (serverSideEnd > -1)
serverSideEnd += serverEnd.length();
int clientMatchEnd = matcher.find(validJSend) ? matcher.end() : -1;
// update end of what we skipped
validJSstart = -1;
if (clientMatchEnd > validJSend && serverSideEnd > validJSend) {
if (biasClient)
validJSstart = clientMatchEnd;
else if (biasServer)
validJSstart = serverSideEnd;
else
validJSstart = Math.min(clientMatchEnd, serverSideEnd);
}
if (clientMatchEnd >= validJSend && serverSideEnd < 0)
validJSstart = matcher.end();
if (clientMatchEnd < 0 && serverSideEnd >= validJSend)
validJSstart = serverSideEnd;
/*
* the substitution text length much match original
* length exactly, generate text of the right length
*/
int start = scriptStartOffset + validJSend;
int end = scriptStartOffset + validJSstart;
generatedContent.append('_');
for (int i = validJSend + 1; i < validJSstart; i++) {
switch(i - validJSend) {
case 1:
generatedContent.append('$');
break;
case 2:
generatedContent.append('t');
break;
case 3:
generatedContent.append('a');
break;
case 4:
generatedContent.append('g');
break;
default:
generatedContent.append('_');
}
}
/*
* Remember this source range, it may be needed to
* find the original contents for which we're
* placeholding
*/
fGeneratedRanges.add(new Region(start, end - start));
appendAndTrack(generatedContent.toString(), start);
// reset now that it's been appended
generatedContent.setLength(0);
}
// set up to end while if no end for valid
if (validJSstart > 0) {
int serverSideStartGuess = -1;
for (int i = 0; i < fServerSideDelimiters.length; i++) {
int index = regionText.indexOf(fServerSideDelimiters[i][0], validJSstart);
if (serverSideStartGuess < 0) {
serverSideStartGuess = index;
serverSideDelimiter = i;
} else if (index >= 0) {
serverSideStartGuess = Math.min(serverSideStartGuess, index);
if (serverSideStartGuess == index) {
serverSideDelimiter = i;
}
}
}
serverSideStart = validJSstart < regionLength - fShortestServerSideDelimiterPairLength ? serverSideStartGuess : -1;
clientMatchStart = validJSstart < regionLength ? (matcher.find(validJSstart) ? matcher.start() : -1) : -1;
} else {
serverSideStart = clientMatchStart = -1;
}
}
if (validJSstart >= 0) {
appendAndTrack(regionText.substring(validJSstart), scriptStartOffset + validJSstart);
Position inHtml = new Position(scriptStartOffset + validJSstart, regionText.length() - validJSstart);
scriptLocationInHtml.add(inHtml);
} else {
appendAndTrack(regionText, scriptStartOffset);
Position inHtml = new Position(scriptStartOffset, regionText.length());
scriptLocationInHtml.add(inHtml);
}
scriptOffset = fScriptText.length();
}
}
IStructuredDocumentRegion endTag = container.getNext();
if (endTag == null) {
missingEndTagRegionStart = container.getStartOffset();
} else if (endTag != null) {
NodeHelper nh = new NodeHelper(endTag);
String name = nh.getTagName();
if (name == null || !name.trim().equalsIgnoreCase("script") || !nh.isEndTag()) {
// $NON-NLS-1$
missingEndTagRegionStart = container.getStartOffset();
}
}
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class HTMLSyntaxColoringPage method getNamedStyleAtOffset.
private String getNamedStyleAtOffset(int offset) {
// ensure the offset is clean
if (offset >= fDocument.getLength())
return getNamedStyleAtOffset(fDocument.getLength() - 1);
else if (offset < 0)
return getNamedStyleAtOffset(0);
IStructuredDocumentRegion documentRegion = fDocument.getFirstStructuredDocumentRegion();
while (documentRegion != null && !documentRegion.containsOffset(offset)) {
documentRegion = documentRegion.getNext();
}
if (documentRegion != null) {
// find the ITextRegion's Context at this offset
ITextRegion interest = documentRegion.getRegionAtCharacterOffset(offset);
if (interest == null)
return null;
if (offset > documentRegion.getTextEndOffset(interest))
return null;
String regionContext = interest.getType();
if (regionContext == null)
return null;
// find the named style (internal/selectable name) for that
// context
String namedStyle = (String) fContextToStyleMap.get(regionContext);
if (namedStyle != null) {
return namedStyle;
}
}
return null;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class TestRegionChangedEvent method testGetRegion.
public void testGetRegion() {
RegionChangedEvent event = getBasicEvent();
IStructuredDocumentRegion structuredDocumentRegion = event.getStructuredDocumentRegion();
assertEquals(null, structuredDocumentRegion);
}
use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.
the class JSONHoverProcessor method getHoverRegion.
@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
if ((textViewer == null) || (textViewer.getDocument() == null)) {
return null;
}
IStructuredDocumentRegion flatNode = ((IStructuredDocument) textViewer.getDocument()).getRegionAtCharacterOffset(offset);
ITextRegion region = null;
if (flatNode != null) {
region = flatNode.getRegionAtCharacterOffset(offset);
}
if (region != null) {
// only supply hoverhelp for object key, or simple JSON value
String regionType = region.getType();
if ((regionType == JSONRegionContexts.JSON_OBJECT_KEY) || JSONUtil.isJSONSimpleValue(regionType)) {
try {
// check if we are at whitespace before or after line
IRegion line = textViewer.getDocument().getLineInformationOfOffset(offset);
if ((offset > (line.getOffset())) && (offset < (line.getOffset() + line.getLength()))) {
// (whitespace after relevant info)
if (offset < flatNode.getTextEndOffset(region)) {
return new Region(flatNode.getStartOffset(region), region.getTextLength());
}
}
} catch (BadLocationException e) {
Logger.logException(e);
}
}
}
return null;
}
Aggregations