use of org.eclipse.wst.jsdt.core.compiler.IProblem 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.core.compiler.IProblem in project webtools.sourceediting by eclipse.
the class JSDTSourceValidator method validateFile.
/**
* @param result
*/
private void validateFile(IValidationContext helper, IReporter reporter, IFile file, ValidationResult result) {
if ((reporter != null) && (reporter.isCancelled() == true)) {
throw new OperationCanceledException();
}
if (!shouldValidate(file)) {
return;
}
char[] source = fDocument.get().toCharArray();
if (source == null) {
return;
}
parser.setSource(source);
parser.setProject(JavaScriptCore.create(file.getProject()));
JavaScriptUnit unit = (JavaScriptUnit) parser.createAST(new NullProgressMonitor());
if (unit == null) {
Message valMessage = new // $NON-NLS-1$
Message(// $NON-NLS-1$
JavaScriptUI.ID_PLUGIN, // $NON-NLS-1$
IMessage.HIGH_SEVERITY, // $NON-NLS-1$
JavaScriptUI.ID_PLUGIN + ".problem") {
/**
* @see IMessage#getText(Locale, ClassLoader)
*/
public java.lang.String getText(Locale locale, ClassLoader classLoader) {
// $NON-NLS-1$
return "AST couldn't be created due to the fatal error";
}
};
valMessage.setOffset(0);
valMessage.setLength(0);
valMessage.setLineNo(0);
reporter.addMessage(this, valMessage);
} else if (unit.getProblems().length > 0) {
for (IProblem problem : unit.getProblems()) {
final String msg = problem.getMessage();
String[] arguments = problem.getArguments();
int severity = problem.isError() ? IMessage.HIGH_SEVERITY : IMessage.ERROR_AND_WARNING;
int lineNumber = problem.getSourceLineNumber();
int sourceStart = problem.getSourceStart();
int sourceEnd = problem.getSourceEnd();
int columnNumber = (problem instanceof DefaultProblem) ? ((DefaultProblem) problem).getSourceColumnNumber() : -1;
Message valMessage = new Message(JavaScriptUI.ID_PLUGIN, severity, JavaScriptUI.ID_PLUGIN + ".problem") {
/**
* @see IMessage#getText(Locale, ClassLoader)
*/
public java.lang.String getText(Locale locale, ClassLoader classLoader) {
return msg;
}
};
Position position = sourceEnd == -1 ? calcPosition(source, sourceStart) : new Position(sourceStart, sourceEnd - sourceStart);
valMessage.setOffset(position.getOffset());
valMessage.setLength(position.getLength());
valMessage.setLineNo(lineNumber);
// System.out.println(getClass().getName() + ": " + valMessage.getLineNumber() +
// "[" + valMessage.getOffset() + ":" + valMessage.getLength() + "] : " +
// valMessage.getText() +
// "==>>>" + String.copyValueOf(source, position.getOffset(), position.getLength()) + "<<<==");
reporter.addMessage(this, valMessage);
}
}
}
use of org.eclipse.wst.jsdt.core.compiler.IProblem in project webtools.sourceediting by eclipse.
the class ShowTranslationHandler method execute.
/*
* (non-Javadoc)
*
* @see
* org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands
* .ExecutionEvent)
*/
public Object execute(final ExecutionEvent event) throws ExecutionException {
// IDE.openEditor(event.getApplicationContext(), createEditorInput(),
// JavaUI.ID_CU_EDITOR, true);
ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection) {
List list = ((IStructuredSelection) selection).toList();
if (!list.isEmpty()) {
if (list.get(0) instanceof IDOMNode) {
final IDOMModel model = ((IDOMNode) list.get(0)).getModel();
INodeAdapter adapter = model.getDocument().getAdapterFor(IJsTranslation.class);
if (adapter != null) {
Job opener = new UIJob("Opening JavaScript Translation") {
public IStatus runInUIThread(IProgressMonitor monitor) {
JsTranslationAdapter translationAdapter = (JsTranslationAdapter) model.getDocument().getAdapterFor(IJsTranslation.class);
final IJsTranslation translation = translationAdapter.getJsTranslation(false);
// create an IEditorInput for the Java editor
final IStorageEditorInput input = new JSTranslationEditorInput(translation, model.getBaseLocation());
try {
IEditorPart editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), input, JavaScriptUI.ID_CU_EDITOR, true);
// Now add the problems we found
if (editor instanceof ITextEditor) {
IAnnotationModel annotationModel = ((ITextEditor) editor).getDocumentProvider().getAnnotationModel(input);
translation.reconcileCompilationUnit();
List problemsList = translation.getProblems();
IProblem[] problems = (IProblem[]) problemsList.toArray(new IProblem[problemsList.size()]);
AnnotationTypeLookup lookup = new AnnotationTypeLookup();
for (int i = 0; i < problems.length; i++) {
int length = problems[i].getSourceEnd() - problems[i].getSourceStart() + 1;
Position position = new Position(problems[i].getSourceStart(), length);
Annotation annotation = null;
String type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_INFO);
if (problems[i].isError()) {
type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_ERROR);
} else if (problems[i].isWarning()) {
type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_WARNING);
}
annotation = new Annotation(type, false, problems[i].getMessage());
if (annotation != null) {
annotationModel.addAnnotation(annotation, position);
}
}
}
} catch (PartInitException e) {
e.printStackTrace();
Display.getCurrent().beep();
}
return Status.OK_STATUS;
}
};
opener.setSystem(false);
opener.setUser(true);
opener.schedule();
}
}
}
}
return null;
}
use of org.eclipse.wst.jsdt.core.compiler.IProblem in project webtools.sourceediting by eclipse.
the class JsTranslation method getProblems.
/* (non-Javadoc)
* @see org.eclipse.wst.jsdt.web.core.internal.java.IJsTranslation#getProblems()
*/
public List getProblems() {
List problemList = getProblemRequestor().getCollectedProblems();
getProblemRequestor().endReporting();
IProblem[] problems = null;
if (problemList == null)
problems = new IProblem[0];
else
problems = (IProblem[]) problemList.toArray(new IProblem[problemList.size()]);
IJsTranslator translator = getTranslator();
if (translator instanceof JsTranslator && problems.length > 0) {
Region[] generatedRanges = ((JsTranslator) translator).getGeneratedRanges();
for (int i = 0; i < problems.length; i++) {
for (int j = 0; j < generatedRanges.length; j++) {
// remove any problems that are fully reported within a region generated by the translator
if (problems[i].getSourceStart() >= generatedRanges[j].getOffset() && problems[i].getSourceEnd() <= (generatedRanges[j].getOffset() + generatedRanges[j].getLength())) {
problemList.remove(problems[i]);
}
}
}
}
return problemList;
}
Aggregations