Search in sources :

Example 6 with TemporaryAnnotation

use of org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation in project webtools.sourceediting by eclipse.

the class SpellcheckStrategy method spellCheck.

private void spellCheck(IRegion dirtyRegion, IRegion regionToBeChecked, IAnnotationModel annotationModel) {
    if (annotationModel == null)
        return;
    TemporaryAnnotation[] annotationsToRemove;
    Annotation[] annotationsToAdd;
    annotationsToRemove = getSpellingAnnotationsToRemove(regionToBeChecked);
    if (_DEBUG_SPELLING_PROBLEMS) {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        Logger.log(Logger.INFO, "Spell checking [" + regionToBeChecked.getOffset() + "-" + (regionToBeChecked.getOffset() + regionToBeChecked.getLength()) + "]");
    }
    if (getDocument() != null) {
        try {
            EditorsUI.getSpellingService().check(getDocument(), new IRegion[] { regionToBeChecked }, fSpellingContext, fProblemCollector, null);
        } finally {
            // corresponding "get" is in #shouldSpellCheck(int)
            if (structuredModel != null) {
                structuredModel.releaseFromRead();
                structuredModel = null;
            }
        }
    }
    annotationsToAdd = fProblemCollector.getAnnotations();
    fProblemCollector.clear();
    if (annotationModel instanceof IAnnotationModelExtension) {
        IAnnotationModelExtension modelExtension = (IAnnotationModelExtension) annotationModel;
        Map annotationsToAddMap = new HashMap();
        for (int i = 0; i < annotationsToAdd.length; i++) {
            annotationsToAddMap.put(annotationsToAdd[i], ((TemporaryAnnotation) annotationsToAdd[i]).getPosition());
        }
        modelExtension.replaceAnnotations(annotationsToRemove, annotationsToAddMap);
    } else {
        for (int j = 0; j < annotationsToAdd.length; j++) {
            annotationModel.addAnnotation(annotationsToAdd[j], ((TemporaryAnnotation) annotationsToAdd[j]).getPosition());
        }
        for (int j = 0; j < annotationsToRemove.length; j++) {
            annotationModel.removeAnnotation(annotationsToRemove[j]);
        }
    }
}
Also used : HashMap(java.util.HashMap) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) HashMap(java.util.HashMap) Map(java.util.Map) Annotation(org.eclipse.jface.text.source.Annotation) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation)

Example 7 with TemporaryAnnotation

use of org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation in project webtools.sourceediting by eclipse.

the class QuickFixProcessorXML method getProposals.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.sse.ui.correction.IQuickFixProcessor#getProposals(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation)
	 */
public ICompletionProposal[] getProposals(Annotation annotation) throws CoreException {
    ArrayList proposals = new ArrayList();
    if (annotation instanceof TemporaryAnnotation) {
        TemporaryAnnotation tempAnnotation = (TemporaryAnnotation) annotation;
        int problemID = tempAnnotation.getProblemID();
        switch(problemID) {
            case ProblemIDsXML.EmptyTag:
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal("", tempAnnotation.getPosition().getOffset(), tempAnnotation.getPosition().getLength(), 0, getImage(), XMLUIMessages.QuickFixProcessorXML_0, null, ""));
                break;
            case ProblemIDsXML.MissingEndTag:
                String tagName = (String) ((Object[]) tempAnnotation.getAdditionalFixInfo())[0];
                String tagClose = (String) ((Object[]) tempAnnotation.getAdditionalFixInfo())[1];
                int tagCloseOffset = ((Integer) ((Object[]) tempAnnotation.getAdditionalFixInfo())[2]).intValue();
                int startTagEndOffset = ((Integer) ((Object[]) tempAnnotation.getAdditionalFixInfo())[3]).intValue();
                int firstChildStartOffset = ((Integer) ((Object[]) tempAnnotation.getAdditionalFixInfo())[4]).intValue();
                int endOffset = ((Integer) ((Object[]) tempAnnotation.getAdditionalFixInfo())[5]).intValue();
                // $NON-NLS-1$
                proposals.add(new CompletionProposal(tagClose, tagCloseOffset, 0, 0, getImage(), XMLUIMessages.QuickFixProcessorXML_1, null, ""));
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal("", tempAnnotation.getPosition().getOffset(), startTagEndOffset - tempAnnotation.getPosition().getOffset(), 0, getImage(), XMLUIMessages.QuickFixProcessorXML_2, null, ""));
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                proposals.add(new CompletionProposal("</" + tagName + ">", firstChildStartOffset, 0, 0, getImage(), XMLUIMessages.QuickFixProcessorXML_3, null, ""));
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                proposals.add(new CompletionProposal("</" + tagName + ">", endOffset, 0, 0, getImage(), XMLUIMessages.QuickFixProcessorXML_4, null, ""));
                break;
            case ProblemIDsXML.AttrsInEndTag:
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal("", tempAnnotation.getPosition().getOffset(), tempAnnotation.getPosition().getLength(), 0, getImage(), XMLUIMessages.QuickFixProcessorXML_5, null, ""));
                break;
            case ProblemIDsXML.MissingAttrValue:
                String defaultAttrValue = (String) ((Object[]) tempAnnotation.getAdditionalFixInfo())[0];
                int insertOffset = ((Integer) ((Object[]) tempAnnotation.getAdditionalFixInfo())[1]).intValue();
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                proposals.add(new CompletionProposal("\"" + defaultAttrValue + "\"", tempAnnotation.getPosition().getOffset() + tempAnnotation.getPosition().getLength() + insertOffset, 0, defaultAttrValue.length() + 2, getImage(), XMLUIMessages.QuickFixProcessorXML_6, null, ""));
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal("", tempAnnotation.getPosition().getOffset(), tempAnnotation.getPosition().getLength(), 0, getImage(), XMLUIMessages.QuickFixProcessorXML_7, null, ""));
                break;
            case ProblemIDsXML.NoAttrValue:
                defaultAttrValue = (String) tempAnnotation.getAdditionalFixInfo();
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                proposals.add(new CompletionProposal("=\"" + defaultAttrValue + "\"", tempAnnotation.getPosition().getOffset() + tempAnnotation.getPosition().getLength(), 0, defaultAttrValue.length() + 3, getImage(), XMLUIMessages.QuickFixProcessorXML_6, null, ""));
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal("", tempAnnotation.getPosition().getOffset(), tempAnnotation.getPosition().getLength(), 0, getImage(), XMLUIMessages.QuickFixProcessorXML_7, null, ""));
                break;
            case ProblemIDsXML.SpacesBeforeTagName:
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal("", tempAnnotation.getPosition().getOffset(), tempAnnotation.getPosition().getLength(), 0, getImage(), XMLUIMessages.QuickFixProcessorXML_8, null, ""));
                break;
            case ProblemIDsXML.SpacesBeforePI:
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal("", tempAnnotation.getPosition().getOffset(), tempAnnotation.getPosition().getLength(), 0, getImage(), XMLUIMessages.QuickFixProcessorXML_9, null, ""));
                break;
            case ProblemIDsXML.NamespaceInPI:
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal("", tempAnnotation.getPosition().getOffset(), tempAnnotation.getPosition().getLength(), 0, getImage(), XMLUIMessages.QuickFixProcessorXML_10, null, ""));
                break;
            case ProblemIDsXML.UnknownElement:
                proposals.add(new RemoveUnknownElementQuickFixProposal(tempAnnotation.getAdditionalFixInfo(), getImage(), XMLUIMessages.QuickFixProcessorXML_11));
                proposals.add(new RenameInFileQuickAssistProposal());
                break;
            case ProblemIDsXML.UnknownAttr:
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal("", tempAnnotation.getPosition().getOffset(), tempAnnotation.getPosition().getLength(), 0, getImage(), XMLUIMessages.QuickFixProcessorXML_7, null, ""));
                proposals.add(new RenameInFileQuickAssistProposal());
                break;
            case ProblemIDsXML.InvalidAttrValue:
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal("", tempAnnotation.getPosition().getOffset(), tempAnnotation.getPosition().getLength(), 0, getImage(), XMLUIMessages.QuickFixProcessorXML_12, null, ""));
                break;
            case ProblemIDsXML.MissingRequiredAttr:
                String requiredAttr = (String) ((Object[]) tempAnnotation.getAdditionalFixInfo())[0];
                insertOffset = ((Integer) ((Object[]) tempAnnotation.getAdditionalFixInfo())[1]).intValue();
                // $NON-NLS-1$
                proposals.add(new CompletionProposal(requiredAttr, tempAnnotation.getPosition().getOffset() + insertOffset, 0, requiredAttr.length(), getImage(), XMLUIMessages.QuickFixProcessorXML_13, null, ""));
                break;
            case ProblemIDsXML.AttrValueNotQuoted:
                String attrValue = (String) tempAnnotation.getAdditionalFixInfo();
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                proposals.add(new CompletionProposal("\"" + attrValue + "\"", tempAnnotation.getPosition().getOffset(), tempAnnotation.getPosition().getLength(), attrValue.length() + 2, getImage(), XMLUIMessages.QuickFixProcessorXML_14, null, ""));
                break;
            case ProblemIDsXML.MissingClosingBracket:
                // $NON-NLS-1$ //$NON-NLS-2$
                proposals.add(new CompletionProposal(">", tempAnnotation.getPosition().getOffset() + tempAnnotation.getPosition().getLength(), 0, 1, getImage(), XMLUIMessages.QuickFixProcessorXML_15, null, ""));
                break;
        }
    }
    return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) CompletionProposal(org.eclipse.jface.text.contentassist.CompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation)

Example 8 with TemporaryAnnotation

use of org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation in project webtools.sourceediting by eclipse.

the class QuickFixProcessorXML method canFix.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.sse.ui.correction.IQuickFixProcessor#canFix(int)
	 */
public boolean canFix(Annotation annotation) {
    boolean result = false;
    if (annotation instanceof TemporaryAnnotation) {
        TemporaryAnnotation tempAnnotation = (TemporaryAnnotation) annotation;
        int problemID = tempAnnotation.getProblemID();
        switch(problemID) {
            case ProblemIDsXML.EmptyTag:
            case ProblemIDsXML.MissingEndTag:
            case ProblemIDsXML.AttrsInEndTag:
            case ProblemIDsXML.MissingAttrValue:
            case ProblemIDsXML.NoAttrValue:
            case ProblemIDsXML.SpacesBeforeTagName:
            case ProblemIDsXML.SpacesBeforePI:
            case ProblemIDsXML.NamespaceInPI:
            case ProblemIDsXML.UnknownElement:
            case ProblemIDsXML.UnknownAttr:
            case ProblemIDsXML.InvalidAttrValue:
            case ProblemIDsXML.MissingRequiredAttr:
            case ProblemIDsXML.AttrValueNotQuoted:
            case ProblemIDsXML.MissingClosingBracket:
                result = true;
        }
    }
    return result;
}
Also used : TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation)

Example 9 with TemporaryAnnotation

use of org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation in project webtools.sourceediting by eclipse.

the class ValidatorStrategy method reconcile.

/**
 * @param tr
 *            Partition of the region to reconcile.
 * @param dr
 *            Dirty region representation of the typed region
 */
public void reconcile(ITypedRegion tr, DirtyRegion dr) {
    /*
		 * Abort if no workspace file is known (new validation framework does
		 * not support that scenario) or no validators have been specified
		 */
    if (isCanceled() || fMetaData.isEmpty() || fValidatorsSuspended)
        return;
    IDocument doc = getDocument();
    // for external files, this can be null
    if (doc == null)
        return;
    String partitionType = tr.getType();
    ValidatorMetaData vmd = null;
    List annotationsToAdd = new ArrayList();
    List stepsRanOnThisDirtyRegion = new ArrayList(1);
    /*
		 * Keep track of the disabled validators by source id for the V2
		 * validators.
		 */
    Set disabledValsBySourceId = new HashSet(20);
    /*
		 * Keep track of the disabled validators by class id for the v1
		 * validators.
		 */
    Set disabledValsByClass = new HashSet(20);
    IFile file = getFile();
    if (file != null) {
        if (!file.isAccessible())
            return;
        Collection disabledValidators = null;
        try {
            /*
				 * Take extra care when calling this external code, as it
				 * can indirectly cause bundles to start
				 */
            disabledValidators = ValidationFramework.getDefault().getDisabledValidatorsFor(file);
        } catch (Exception e) {
            Logger.logException(e);
        }
        if (disabledValidators != null) {
            for (Iterator it = disabledValidators.iterator(); it.hasNext(); ) {
                Validator v = (Validator) it.next();
                Validator.V1 v1 = null;
                try {
                    v1 = v.asV1Validator();
                } catch (Exception e) {
                    Logger.logException(e);
                }
                if (v1 != null)
                    disabledValsByClass.add(v1.getId());
                else // not a V1 validator
                if (v.getSourceId() != null) {
                    // could be more then one sourceid per batch validator
                    String[] sourceIDs = StringUtils.unpack(v.getSourceId());
                    disabledValsBySourceId.addAll(Arrays.asList(sourceIDs));
                }
            }
        }
    }
    /*
		 * Loop through all of the relevant validator meta data to find
		 * supporting validators for this partition type. Don't check
		 * this.canHandlePartition() before-hand since it just loops through
		 * and calls vmd.canHandlePartitionType()...which we're already doing
		 * here anyway to find the right vmd.
		 */
    for (int i = 0; i < fMetaData.size() && !isCanceled(); i++) {
        vmd = (ValidatorMetaData) fMetaData.get(i);
        if (vmd.canHandlePartitionType(getContentTypeIds(), partitionType)) {
            /*
				 * Check if validator is enabled according to validation
				 * preferences before attempting to create/use it
				 */
            if (!disabledValsBySourceId.contains(vmd.getValidatorId()) && !disabledValsByClass.contains(vmd.getValidatorClass())) {
                if (DEBUG_VALIDATION_UNSUPPORTED) {
                    Logger.log(Logger.INFO, "Source validator " + vmd.getValidatorId() + " handling (content types:[" + StringUtils.pack(getContentTypeIds()) + "] partition type:" + partitionType);
                }
                int validatorScope = vmd.getValidatorScope();
                ReconcileStepForValidator validatorStep = null;
                // get step for partition type
                Object o = fVidToVStepMap.get(vmd.getValidatorId());
                if (o != null) {
                    validatorStep = (ReconcileStepForValidator) o;
                } else {
                    // if doesn't exist, create one
                    IValidator validator = vmd.createValidator();
                    validatorStep = new ReconcileStepForValidator(validator, validatorScope);
                    validatorStep.setInputModel(new DocumentAdapter(doc));
                    fVidToVStepMap.put(vmd.getValidatorId(), validatorStep);
                }
                if (!fTotalScopeValidatorsAlreadyRun.contains(vmd) && !fIsCancelled) {
                    annotationsToAdd.addAll(Arrays.asList(validatorStep.reconcile(dr, dr)));
                    stepsRanOnThisDirtyRegion.add(validatorStep);
                    if (validatorScope == ReconcileAnnotationKey.TOTAL) {
                        // mark this validator as "run"
                        fTotalScopeValidatorsAlreadyRun.add(vmd);
                    }
                }
            } else if (DEBUG_VALIDATION_CAPABLE_BUT_DISABLED) {
                String message = "Source validator able (id:" + vmd.getValidatorId() + " class:" + vmd.getValidatorClass() + " but skipped because it was reported as disabled";
                Logger.log(Logger.INFO, message);
            }
        } else if (DEBUG_VALIDATION_UNSUPPORTED) {
            Logger.log(Logger.INFO, "Source validator " + vmd.getValidatorId() + " can not handle (content types:[" + StringUtils.pack(getContentTypeIds()) + "] partition type:" + partitionType);
        }
    }
    TemporaryAnnotation[] annotationsToRemove = getAnnotationsToRemove(dr, stepsRanOnThisDirtyRegion);
    if (annotationsToRemove.length + annotationsToAdd.size() > 0 && !fIsCancelled)
        smartProcess(annotationsToRemove, (IReconcileResult[]) annotationsToAdd.toArray(new IReconcileResult[annotationsToAdd.size()]));
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) DocumentAdapter(org.eclipse.wst.sse.ui.internal.reconcile.DocumentAdapter) IValidator(org.eclipse.wst.validation.internal.provisional.core.IValidator) IReconcileResult(org.eclipse.jface.text.reconciler.IReconcileResult) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IDocument(org.eclipse.jface.text.IDocument) IValidator(org.eclipse.wst.validation.internal.provisional.core.IValidator) Validator(org.eclipse.wst.validation.Validator) HashSet(java.util.HashSet)

Example 10 with TemporaryAnnotation

use of org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation in project webtools.sourceediting by eclipse.

the class HTMLAttributeValidationQuickFixProcessor method canFix.

/*
	 * @see org.eclipse.jface.text.quickassist.IQuickAssistProcessor#canFix(org.eclipse.jface.text.source.Annotation)
	 */
public boolean canFix(Annotation annotation) {
    boolean result = false;
    String text = null;
    if (annotation instanceof TemporaryAnnotation) {
        TemporaryAnnotation tempAnnotation = (TemporaryAnnotation) annotation;
        int problemID = tempAnnotation.getProblemID();
        text = tempAnnotation.getText();
        if (problemID == 0 && text != null)
            result = true;
    } else if (annotation instanceof MarkerAnnotation) {
        MarkerAnnotation markerAnnotation = (MarkerAnnotation) annotation;
        text = markerAnnotation.getText();
        IMarker marker = markerAnnotation.getMarker();
        IResource resource = marker == null ? null : marker.getResource();
        if (resource != null && resource.exists() && resource.isAccessible() && text != null) {
            result = true;
        }
    }
    result = (result && UNDEFINED_ATTRIBUTE_NAME_MATCHER.match(text));
    return result;
}
Also used : MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) IMarker(org.eclipse.core.resources.IMarker) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IResource(org.eclipse.core.resources.IResource)

Aggregations

TemporaryAnnotation (org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation)13 ArrayList (java.util.ArrayList)7 List (java.util.List)6 Iterator (java.util.Iterator)5 Annotation (org.eclipse.jface.text.source.Annotation)5 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)5 MarkerAnnotation (org.eclipse.ui.texteditor.MarkerAnnotation)5 IResource (org.eclipse.core.resources.IResource)4 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)4 IAnnotationModelExtension2 (org.eclipse.jface.text.source.IAnnotationModelExtension2)4 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Set (java.util.Set)3 Position (org.eclipse.jface.text.Position)3 IMarker (org.eclipse.core.resources.IMarker)2 IProject (org.eclipse.core.resources.IProject)2 ProjectScope (org.eclipse.core.resources.ProjectScope)2 IAdaptable (org.eclipse.core.runtime.IAdaptable)2 DefaultScope (org.eclipse.core.runtime.preferences.DefaultScope)2