Search in sources :

Example 1 with IProblem

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);
                    }
                }
            }
        }
    }
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IProblem(org.eclipse.wst.jsdt.core.compiler.IProblem) CoreException(org.eclipse.core.runtime.CoreException) ArrayList(java.util.ArrayList) List(java.util.List) IMarker(org.eclipse.core.resources.IMarker)

Example 2 with IProblem

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);
        }
    }
}
Also used : Locale(java.util.Locale) JavaScriptUnit(org.eclipse.wst.jsdt.core.dom.JavaScriptUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) Message(org.eclipse.wst.validation.internal.core.Message) Position(org.eclipse.jface.text.Position) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IProblem(org.eclipse.wst.jsdt.core.compiler.IProblem) DefaultProblem(org.eclipse.wst.jsdt.internal.compiler.problem.DefaultProblem)

Example 3 with IProblem

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;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Position(org.eclipse.jface.text.Position) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IEditorPart(org.eclipse.ui.IEditorPart) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IProblem(org.eclipse.wst.jsdt.core.compiler.IProblem) Annotation(org.eclipse.jface.text.source.Annotation) AnnotationTypeLookup(org.eclipse.ui.texteditor.AnnotationTypeLookup) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ISelection(org.eclipse.jface.viewers.ISelection) UIJob(org.eclipse.ui.progress.UIJob) List(java.util.List) PartInitException(org.eclipse.ui.PartInitException) Job(org.eclipse.core.runtime.jobs.Job) UIJob(org.eclipse.ui.progress.UIJob)

Example 4 with IProblem

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;
}
Also used : Region(org.eclipse.jface.text.Region) List(java.util.List) IProblem(org.eclipse.wst.jsdt.core.compiler.IProblem)

Aggregations

IProblem (org.eclipse.wst.jsdt.core.compiler.IProblem)4 List (java.util.List)3 Position (org.eclipse.jface.text.Position)2 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)2 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)2 IMessage (org.eclipse.wst.validation.internal.provisional.core.IMessage)2 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)2 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 IMarker (org.eclipse.core.resources.IMarker)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 Job (org.eclipse.core.runtime.jobs.Job)1 Region (org.eclipse.jface.text.Region)1 Annotation (org.eclipse.jface.text.source.Annotation)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 ISelection (org.eclipse.jface.viewers.ISelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1