use of org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation in project webtools.sourceediting by eclipse.
the class TestHtmlTranslation method testCDATAAroundJS3.
public void testCDATAAroundJS3() {
// get model
String fileName = getName() + ".html";
IStructuredModel structuredModel = getSharedModel(fileName, "<script>//<![CDATA[\n var text = serverObject.getText(); ]]></script>");
assertNotNull("missing test model", structuredModel);
// do translation
JsTranslationAdapterFactory.setupAdapterFactory(structuredModel);
JsTranslationAdapter translationAdapter = (JsTranslationAdapter) ((IDOMModel) structuredModel).getDocument().getAdapterFor(IJsTranslation.class);
IJsTranslation translation = translationAdapter.getJsTranslation(false);
String translated = translation.getJsText();
assertTrue("translation empty", translated.length() > 5);
assertTrue("CDATA start found", translated.indexOf("CDATA") < 0);
assertTrue("CDATA start found", translated.indexOf("[") < 0);
assertTrue("CDATA end found", translated.indexOf("]") < 0);
assertTrue("problems found in translation ", translation.getProblems().isEmpty());
// release model
structuredModel.releaseFromRead();
}
use of org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation in project webtools.sourceediting by eclipse.
the class JSDTHoverProcessor method getHoverInfo.
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
* org.eclipse.jface.text.IRegion)
*/
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
// get JSP translation object for this viewer's document
IDOMModel xmlModel = null;
try {
xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(textViewer.getDocument());
if (xmlModel != null) {
IDOMDocument xmlDoc = xmlModel.getDocument();
JsTranslationAdapter adapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (adapter != null) {
try {
boolean proceed = false;
ITypedRegion[] partitions = xmlDoc.getStructuredDocument().computePartitioning(hoverRegion.getOffset(), hoverRegion.getLength());
for (int i = 0; i < partitions.length; i++) {
for (int j = 0; j < PARTITION_TYPES.length; j++) {
if (PARTITION_TYPES[j].equals(partitions[i].getType())) {
proceed = true;
break;
}
}
}
if (proceed) {
IJsTranslation translation = adapter.getJsTranslation(true);
IJavaScriptElement[] result = translation.getElementsFromJsRange(translation.getJavaScriptOffset(hoverRegion.getOffset()), translation.getJavaScriptOffset(hoverRegion.getOffset() + hoverRegion.getLength()));
return translation.fixupMangledName(getHoverInfo(result));
}
} catch (BadLocationException e) {
// do nothing
}
}
}
} finally {
if (xmlModel != null) {
xmlModel.releaseFromRead();
}
}
return null;
}
use of org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation in project webtools.sourceediting by eclipse.
the class JFaceNodeAdapterForJs method getJSElementsFromNode.
private synchronized Object[] getJSElementsFromNode(Node node) {
if (node == null) {
return new Object[0];
}
int startOffset = 0;
int endOffset = 0;
IJavaScriptElement[] result = new IJavaScriptElement[0];
IJsTranslation translation = null;
if (node.getNodeType() == Node.TEXT_NODE && (node instanceof NodeImpl)) {
translation = getTranslation(node);
startOffset = translation.getJavaScriptOffset(((NodeImpl) node).getStartOffset());
endOffset = translation.getJavaScriptOffset(((NodeImpl) node).getEndOffset() - 1);
if (startOffset >= 0 && endOffset >= 0)
result = translation.getAllElementsInJsRange(startOffset, endOffset);
}
return result;
//
// if (result == null) return null;
// Object[] newResults = new Object[result.length];
// for (int i = 0; i < result.length; i++) {
// int htmllength = 0;
// int htmloffset = 0;
// Position position = null;
// try {
// htmllength = ((SourceRefElement) (result[i])).getSourceRange().getLength();
// htmloffset = translation.getJspOffset(((SourceRefElement)
// (result[i])).getSourceRange().getOffset());
// position = new Position(htmloffset, htmllength);
// } catch (JavaScriptModelException e) {
// e.printStackTrace();
// }
// newResults[i] = getJsNode(node.getParentNode(), (IJavaScriptElement) result[i],
// position);
// }
// return newResults;
}
use of org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation in project webtools.sourceediting by eclipse.
the class FormattingStrategyJSDT method getTranslation.
public IJsTranslation getTranslation(IStructuredDocument document) {
IJsTranslation tran = null;
IDOMModel xmlModel = null;
try {
xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(document);
IDOMDocument xmlDoc = xmlModel.getDocument();
JsTranslationAdapterFactory.setupAdapterFactory(xmlModel);
JsTranslationAdapter translationAdapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (translationAdapter != null) {
tran = translationAdapter.getJsTranslation(true);
}
} finally {
if (xmlModel != null) {
xmlModel.releaseFromRead();
}
}
return tran;
}
use of org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation in project webtools.sourceediting by eclipse.
the class JSDTHyperlinkDetector method detectHyperlinks.
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer,
* org.eclipse.jface.text.IRegion, boolean)
*/
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
List hyperlinks = new ArrayList(0);
if (region != null && textViewer != null) {
IDocument document = textViewer.getDocument();
try {
boolean proceed = false;
ITypedRegion[] partitions = document.computePartitioning(region.getOffset(), region.getLength());
for (int i = 0; i < partitions.length; i++) {
for (int j = 0; j < PARTITION_TYPES.length; j++) {
if (PARTITION_TYPES[j].equals(partitions[i].getType())) {
proceed = true;
}
}
}
if (proceed) {
IJsTranslation jsTranslation = getJsTranslation(document);
if (jsTranslation != null) {
// find hyperlink range for JavaScript elements
IRegion hyperlinkRegion = selectWord(document, region.getOffset());
IJavaScriptElement[] elements = jsTranslation.getElementsFromJsRange(jsTranslation.getJavaScriptOffset(region.getOffset()), jsTranslation.getJavaScriptOffset(region.getOffset() + region.getLength()));
if (elements != null && elements.length > 0) {
// create a hyperlink for each JavaScript element
for (int i = 0; i < elements.length; ++i) {
IJavaScriptElement element = elements[i];
IHyperlink link = createHyperlink(jsTranslation, element, hyperlinkRegion, document);
if (link != null) {
hyperlinks.add(link);
}
}
}
}
}
} catch (BadLocationException e) {
}
}
if (hyperlinks.size() == 0) {
return null;
}
return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[0]);
}
Aggregations