Add custom validator to specific file type in Eclipse plugin

430 Views Asked by At

I created a custom editor for my file type and now I need to add validation to it to display errors in the Problems view, so first I created a new marker type:

<extension
    point="org.eclipse.core.resources.markers"
    id="my.marker"
    name="My Marker">
    <super
        type="org.eclipse.core.resources.problemmarker">
    </super>
    <persistent
        value="true">
    </persistent>
</extension>

Then I added the validator:

<extension
    point="org.eclipse.wst.validation.validatorV2"
    id="my.validator"
    name="My Validator">
    <validator
        class="com.test.MyValidator"
        markerId="my.marker"
        build="true"
        manual="true">
        <include>
            <rules>
                <fileext
                    ext="mpe">
                </fileext>
            </rules>
        </include>
    </validator>
</extension>

Finally I created the MyValidator class that extends AbstractValidator, then I overrided the validate method, but looks like this class/method is never even invoked, breakpoints in it are not hit:

public class MyValidator extends AbstractValidator {
    @Override
    public ValidationResult validate(ValidationEvent event, ValidationState state, IProgressMonitor monitor) {
        // Validation logic
    }
}

enter image description here

Is this the correct way to add validation to a file? Not sure if I should use builders as I'm not building anything and looks like builders are attached to an specific project nature and I need the validation to be attached to a file type regardless of the type of project it is in.

UPDATE

Looks like the validate method is only called when I right click the file > Validate, I thought the validation will happen automatically when the file is modified, is there a way to do that?

0

There are 0 best solutions below