use of org.eclipse.wst.jsdt.core.IJavaScriptUnit in project webtools.sourceediting by eclipse.
the class JSDTHyperlinkDetector method createHyperlink.
private IHyperlink createHyperlink(IJsTranslation jsTranslation, IJavaScriptElement element, IRegion region, IDocument document) {
IHyperlink link = null;
if (region != null) {
// open local variable in the JSP file...
if (element instanceof ISourceReference) {
IFile file = null;
IPath outsidePath = null;
int jspOffset = 0;
IStructuredModel sModel = null;
// try to locate the file in the workspace
try {
sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (sModel != null) {
// URIResolver resolver = sModel.getResolver();
// if (resolver != null) {
// String uriString = resolver.getFileBaseLocation();
String uriString = sModel.getBaseLocation();
file = getFile(uriString);
// }
}
} finally {
if (sModel != null) {
sModel.releaseFromRead();
}
}
// get Java range, translate coordinate to JSP
try {
ISourceRange range = null;
if (jsTranslation != null) {
// link to local variable definitions
if (element instanceof ILocalVariable) {
range = ((ILocalVariable) element).getNameRange();
IJavaScriptElement unit = ((ILocalVariable) element).getParent();
IJavaScriptUnit myUnit = jsTranslation.getCompilationUnit();
while (!(unit instanceof IJavaScriptUnit || unit instanceof IClassFile || unit == null)) {
unit = ((JavaElement) unit).getParent();
}
if (unit instanceof IJavaScriptUnit) {
IJavaScriptUnit cu = (IJavaScriptUnit) unit;
if (cu != myUnit) {
file = getFile(cu.getPath().toString());
if (file == null) {
outsidePath = cu.getPath();
}
}
} else if (unit instanceof IClassFile) {
IClassFile cu = (IClassFile) unit;
if (cu != myUnit) {
file = getFile(cu.getPath().toString());
if (file == null) {
outsidePath = cu.getPath();
}
}
}
} else // linking to fields of the same compilation unit
if (element.getElementType() == IJavaScriptElement.FIELD) {
Object cu = ((IField) element).getJavaScriptUnit();
if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
range = ((ISourceReference) element).getSourceRange();
}
} else // linking to methods of the same compilation unit
if (element.getElementType() == IJavaScriptElement.METHOD) {
Object cu = ((IFunction) element).getJavaScriptUnit();
if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
range = ((ISourceReference) element).getSourceRange();
}
}
}
if (range != null && file != null) {
jspOffset = range.getOffset();
if (jspOffset >= 0) {
link = new WorkspaceFileHyperlink(region, file, new Region(jspOffset, range.getLength()));
}
} else if (range != null && outsidePath != null) {
jspOffset = range.getOffset();
if (jspOffset >= 0) {
link = new ExternalFileHyperlink(region, outsidePath.toFile());
}
}
} catch (JavaScriptModelException jme) {
Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
}
}
if (link == null) {
link = new JSDTHyperlink(region, element);
}
}
return link;
}
use of org.eclipse.wst.jsdt.core.IJavaScriptUnit in project webtools.sourceediting by eclipse.
the class JSDTContentAssistantProcessor method computeCompletionProposals.
/**
* Returns a list of completion proposals based on the specified location
* within the document that corresponds to the current cursor position
* within the text viewer.
*
* @param viewer
* the viewer whose document is used to compute the proposals
* @param documentPosition
* an offset within the document for which completions should be
* computed
* @return an array of completion proposals or <code>null</code> if no
* proposals are possible
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int pos) {
initialize(pos);
JSDTProposalCollector collector = null;
IDOMModel xmlModel = null;
try {
fViewer = viewer;
xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(fViewer.getDocument());
IDOMDocument xmlDoc = xmlModel.getDocument();
JsTranslationAdapterFactory.setupAdapterFactory(xmlModel);
JsTranslationAdapter translationAdapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (translationAdapter != null) {
IJsTranslation translation = translationAdapter.getJsTranslation(true);
fJavaPosition = translation.getJavaScriptOffset(getDocumentPosition());
try {
IJavaScriptUnit cu = translation.getCompilationUnit();
// or without a valid position
if (cu == null || -1 == fJavaPosition) {
return new ICompletionProposal[0];
}
collector = getProposalCollector();
synchronized (cu) {
cu.codeComplete(fJavaPosition, collector, null);
}
} catch (CoreException coreEx) {
// a possible Java Model Exception due to not being a Web
// (Java) Project
coreEx.printStackTrace();
}
}
} catch (Exception exc) {
exc.printStackTrace();
// throw out exceptions on code assist.
} finally {
if (xmlModel != null) {
xmlModel.releaseFromRead();
}
}
ICompletionProposal[] results = new ICompletionProposal[0];
if (collector != null) {
results = collector.getJSPCompletionProposals();
if (results == null || results.length < 1) {
fErrorMessage = JsUIMessages.Java_Content_Assist_is_not_UI_;
}
}
return results;
}
use of org.eclipse.wst.jsdt.core.IJavaScriptUnit in project webtools.sourceediting by eclipse.
the class AddJavaDocStubAction method run.
public void run(IAction action) {
IJavaScriptElement[] elements = JsElementActionProxy.getJsElementsFromSelection(selection);
if (elements == null || elements.length < 1) {
return;
}
IJavaScriptElement parent = elements[0].getParent();
/* find the cu */
while (parent != null && !(parent instanceof IJavaScriptUnit)) {
}
if (parent != null) {
ArrayList members = new ArrayList();
for (int i = 0; i < elements.length; i++) {
if (elements[i] instanceof IMember) {
members.add(elements[i]);
}
}
JsJfaceNode[] node = SimpleJSDTActionProxy.getJsJfaceNodesFromSelection(selection);
/* only should be one node */
run((IJavaScriptUnit) parent, (IMember[]) members.toArray(new IMember[members.size()]), node[0]);
}
}
use of org.eclipse.wst.jsdt.core.IJavaScriptUnit in project webtools.sourceediting by eclipse.
the class JsTranslation method reconcileCompilationUnit.
/* (non-Javadoc)
* @see org.eclipse.wst.jsdt.web.core.internal.java.IJsTranslation#reconcileCompilationUnit()
*/
public void reconcileCompilationUnit() {
// if(true) return;
IJavaScriptUnit cu = getCompilationUnit();
if (fCompilationUnit == null) {
return;
}
if (cu != null) {
try {
synchronized (fLock) {
// clear out old validation messages
WorkingCopyOwner workingCopyOwner = getWorkingCopyOwner();
JsProblemRequestor problemRequestor = (JsProblemRequestor) workingCopyOwner.getProblemRequestor(cu.getWorkingCopy(getProgressMonitor()));
if (problemRequestor != null && problemRequestor.getCollectedProblems() != null)
problemRequestor.getCollectedProblems().clear();
cu.reconcile(IJavaScriptUnit.NO_AST, true, true, getWorkingCopyOwner(), getProgressMonitor());
}
} catch (JavaScriptModelException e) {
Logger.logException(e);
}
}
}
use of org.eclipse.wst.jsdt.core.IJavaScriptUnit in project webtools.sourceediting by eclipse.
the class JsTranslation method getElementsFromJsRange.
/* (non-Javadoc)
* @see org.eclipse.wst.jsdt.web.core.internal.java.IJsTranslation#getElementsFromJsRange(int, int)
*/
public IJavaScriptElement[] getElementsFromJsRange(int javaPositionStart, int javaPositionEnd) {
IJavaScriptElement[] EMTPY_RESULT_SET = new IJavaScriptElement[0];
IJavaScriptElement[] result = EMTPY_RESULT_SET;
try {
IJavaScriptUnit cu = getCompilationUnit();
if (cu != null) {
synchronized (fLock) {
int cuDocLength = cu.getBuffer().getLength();
int javaLength = javaPositionEnd - javaPositionStart;
if (cuDocLength > 0 && javaPositionStart >= 0 && javaLength >= 0 && javaPositionEnd <= cuDocLength) {
result = cu.codeSelect(javaPositionStart, javaLength, getWorkingCopyOwner());
}
}
}
if (result == null || result.length == 0) {
return EMTPY_RESULT_SET;
}
} catch (JavaScriptModelException x) {
Logger.logException(x);
}
return result;
}
Aggregations