use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.
the class JsValidator method performValidation.
void performValidation(IFile f, IReporter reporter, IStructuredModel model, boolean inBatch) {
if (model instanceof IDOMModel) {
IDOMModel domModel = (IDOMModel) model;
JsTranslationAdapterFactory.setupAdapterFactory(domModel);
IDOMDocument xmlDoc = domModel.getDocument();
JsTranslationAdapter translationAdapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
// translationAdapter.resourceChanged();
IJsTranslation translation = translationAdapter.getJsTranslation(false);
if (!reporter.isCancelled()) {
translation.setProblemCollectingActive(true);
translation.reconcileCompilationUnit();
List problems = translation.getProblems();
// only update task markers if the model is the same as what's on disk
boolean updateTasks = !domModel.isDirty() && f != null && f.isAccessible();
if (updateTasks) {
// remove old JavaScript task markers
try {
IMarker[] foundMarkers = f.findMarkers(JAVASCRIPT_TASK_MARKER_TYPE, true, IResource.DEPTH_ONE);
for (int i = 0; i < foundMarkers.length; i++) {
foundMarkers[i].delete();
}
} catch (CoreException e) {
Logger.logException(e);
}
}
// add new messages
for (int i = 0; i < problems.size() && !reporter.isCancelled(); i++) {
IProblem problem = (IProblem) problems.get(i);
IMessage m = createMessageFromProblem(problem, f, translation, domModel.getStructuredDocument());
if (m != null) {
if (problem.getID() == IProblem.Task) {
if (updateTasks) {
// add new JavaScript task marker
try {
IMarker task = f.createMarker(JAVASCRIPT_TASK_MARKER_TYPE);
task.setAttribute(IMarker.LINE_NUMBER, new Integer(m.getLineNumber()));
task.setAttribute(IMarker.CHAR_START, new Integer(m.getOffset()));
task.setAttribute(IMarker.CHAR_END, new Integer(m.getOffset() + m.getLength()));
task.setAttribute(IMarker.MESSAGE, m.getText());
task.setAttribute(IMarker.USER_EDITABLE, Boolean.FALSE);
switch(m.getSeverity()) {
case IMessage.HIGH_SEVERITY:
{
task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_HIGH));
task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
}
break;
case IMessage.LOW_SEVERITY:
{
task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_LOW));
task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
}
break;
default:
{
task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_NORMAL));
task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
}
}
} catch (CoreException e) {
Logger.logException(e);
}
}
} else {
reporter.addMessage(fMessageOriginator, m);
}
}
}
}
}
}
use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.
the class JSDTHyperlinkDetector method getJsTranslation.
/**
* Get JSP translation object
*
* @return JSPTranslation if one exists, null otherwise
*/
private IJsTranslation getJsTranslation(IDocument document) {
IJsTranslation translation = null;
IDOMModel xmlModel = null;
try {
xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (xmlModel != null) {
IDOMDocument xmlDoc = xmlModel.getDocument();
JsTranslationAdapter adapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (adapter != null) {
translation = adapter.getJsTranslation(true);
}
}
} finally {
if (xmlModel != null) {
xmlModel.releaseFromRead();
}
}
return translation;
}
use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.
the class JSPJavaSelectionProvider method getSelection.
static IJavaScriptElement[] getSelection(ITextEditor textEditor) {
IJavaScriptElement[] elements = null;
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
ISelection selection = textEditor.getSelectionProvider().getSelection();
if (selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) selection;
// get the JSP translation object for this editor's document
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model instanceof IDOMModel) {
IDOMModel xmlModel = (IDOMModel) model;
IDOMDocument xmlDoc = xmlModel.getDocument();
JsTranslationAdapter adapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (adapter != null) {
IJsTranslation translation = adapter.getJsTranslation(true);
elements = translation.getElementsFromJsRange(translation.getJavaScriptOffset(textSelection.getOffset()), translation.getJavaScriptOffset(textSelection.getOffset() + textSelection.getLength()));
}
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
if (elements == null) {
elements = new IJavaScriptElement[0];
}
return elements;
}
use of org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter 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.web.core.javascript.JsTranslationAdapter in project webtools.sourceediting by eclipse.
the class JsFindOccurrencesProcessor method getJavaElementsForCurrentSelection.
/**
* uses JSPTranslation to get currently selected Java elements.
*
* @return currently selected IJavaElements
*/
private IJavaScriptElement[] getJavaElementsForCurrentSelection(IDocument document, ITextSelection selection) {
IJavaScriptElement[] elements = new IJavaScriptElement[0];
// get JSP translation object for this viewer's document
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model != null && model instanceof IDOMModel) {
IDOMDocument xmlDoc = ((IDOMModel) model).getDocument();
JsTranslationAdapter adapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (adapter != null) {
IJsTranslation translation = adapter.getJsTranslation(false);
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=102211
elements = translation.getElementsFromJsRange(translation.getJavaScriptOffset(selection.getOffset()), translation.getJavaScriptOffset(selection.getOffset() + selection.getLength()));
}
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
return elements;
}
Aggregations