use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.
the class GotoAnnotationAction method getNextAnnotation.
/**
* Returns the annotation closest to the given range respecting the given
* direction. If an annotation is found, the annotations current position
* is copied into the provided annotation position.
*
* @param offset
* the region offset
* @param length
* the region length
* @param forward
* <code>true</code> for forwards, <code>false</code> for
* backward
* @param annotationPosition
* the position of the found annotation
* @return the found annotation
*/
protected Annotation getNextAnnotation(final int offset, final int length, boolean forward, Position annotationPosition) {
Annotation nextAnnotation = null;
Position nextAnnotationPosition = null;
Annotation containingAnnotation = null;
Position containingAnnotationPosition = null;
boolean currentAnnotation = false;
IDocument document = getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
int endOfDocument = document.getLength();
int distance = Integer.MAX_VALUE;
IAnnotationModel model = getTextEditor().getDocumentProvider().getAnnotationModel(getTextEditor().getEditorInput());
// external files may not have an annotation model
if (model != null) {
Iterator e = model.getAnnotationIterator();
while (e.hasNext()) {
Annotation a = (Annotation) e.next();
if (!isNavigationTarget(a))
continue;
Position p = model.getPosition(a);
if (p == null)
continue;
if (forward && p.offset == offset || !forward && p.offset + p.getLength() == offset + length) {
if (containingAnnotation == null || (forward && p.length >= containingAnnotationPosition.length || !forward && p.length >= containingAnnotationPosition.length)) {
containingAnnotation = a;
containingAnnotationPosition = p;
currentAnnotation = p.length == length;
}
} else {
int currentDistance = 0;
if (forward) {
currentDistance = p.getOffset() - offset;
if (currentDistance < 0) {
currentDistance = endOfDocument + currentDistance;
}
if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) {
distance = currentDistance;
nextAnnotation = a;
nextAnnotationPosition = p;
}
} else {
currentDistance = offset + length - (p.getOffset() + p.length);
if (currentDistance < 0)
currentDistance = endOfDocument + currentDistance;
if (currentDistance < distance || currentDistance == distance && p.length < nextAnnotationPosition.length) {
distance = currentDistance;
nextAnnotation = a;
nextAnnotationPosition = p;
}
}
}
}
}
if (containingAnnotationPosition != null && (!currentAnnotation || nextAnnotation == null)) {
annotationPosition.setOffset(containingAnnotationPosition.getOffset());
annotationPosition.setLength(containingAnnotationPosition.getLength());
return containingAnnotation;
}
if (nextAnnotationPosition != null) {
annotationPosition.setOffset(nextAnnotationPosition.getOffset());
annotationPosition.setLength(nextAnnotationPosition.getLength());
}
return nextAnnotation;
}
use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.
the class SourceValidationQuickAssistProcessor method computeQuickAssistProposals.
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext quickAssistContext) {
ISourceViewer viewer = quickAssistContext.getSourceViewer();
int documentOffset = quickAssistContext.getOffset();
int length = viewer != null ? viewer.getSelectedRange().y : 0;
IAnnotationModel model = viewer.getAnnotationModel();
if (model == null)
return null;
List allProposals = new ArrayList();
if (model instanceof IAnnotationModelExtension2) {
Iterator iter = ((IAnnotationModelExtension2) model).getAnnotationIterator(documentOffset, length, true, true);
while (iter.hasNext()) {
List processors = new ArrayList();
Annotation anno = (Annotation) iter.next();
if (canFix(anno)) {
// fix processor attached to it
if (anno instanceof TemporaryAnnotation) {
Object o = ((TemporaryAnnotation) anno).getAdditionalFixInfo();
if (o instanceof IQuickAssistProcessor) {
processors.add(o);
}
}
// get all relevant quick fixes for this annotation
QuickFixRegistry registry = QuickFixRegistry.getInstance();
processors.addAll(Arrays.asList(registry.getQuickFixProcessors(anno)));
// set up context
Map attributes = null;
if (anno instanceof TemporaryAnnotation) {
attributes = ((TemporaryAnnotation) anno).getAttributes();
}
Position pos = model.getPosition(anno);
StructuredTextInvocationContext sseContext = new StructuredTextInvocationContext(viewer, pos.getOffset(), pos.getLength(), attributes);
// call each processor
for (int i = 0; i < processors.size(); ++i) {
List proposals = new ArrayList();
collectProposals((IQuickAssistProcessor) processors.get(i), anno, sseContext, proposals);
if (proposals.size() > 0) {
allProposals.addAll(proposals);
}
}
}
}
}
if (allProposals.isEmpty())
return null;
return (ICompletionProposal[]) allProposals.toArray(new ICompletionProposal[allProposals.size()]);
}
use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.
the class HTMLAttributeValidationQuickFixProcessor method computeQuickAssistProposals.
/*
* @see org.eclipse.jface.text.quickassist.IQuickAssistProcessor#computeQuickAssistProposals(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext)
*/
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
ISourceViewer viewer = invocationContext.getSourceViewer();
int documentOffset = invocationContext.getOffset();
int length = viewer != null ? viewer.getSelectedRange().y : 0;
IAnnotationModel model = viewer.getAnnotationModel();
if (model == null)
return null;
List proposals = new ArrayList();
if (model instanceof IAnnotationModelExtension2) {
Iterator iter = ((IAnnotationModelExtension2) model).getAnnotationIterator(documentOffset, length, true, true);
while (iter.hasNext()) {
Annotation anno = (Annotation) iter.next();
if (canFix(anno)) {
int offset = -1;
if (anno instanceof TemporaryAnnotation) {
offset = ((TemporaryAnnotation) anno).getPosition().getOffset();
} else if (anno instanceof MarkerAnnotation) {
offset = ((MarkerAnnotation) anno).getMarker().getAttribute(IMarker.CHAR_START, -1);
}
if (offset == -1)
continue;
IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, offset);
if (!(node instanceof Element))
continue;
Object adapter = (node instanceof IAdaptable ? ((IAdaptable) node).getAdapter(IResource.class) : null);
IProject project = (adapter instanceof IResource ? ((IResource) adapter).getProject() : null);
IScopeContext[] fLookupOrder = new IScopeContext[] { new InstanceScope(), new DefaultScope() };
if (project != null) {
ProjectScope projectScope = new ProjectScope(project);
if (projectScope.getNode(getPreferenceNodeQualifier()).getBoolean(getProjectSettingsKey(), false))
fLookupOrder = new IScopeContext[] { projectScope, new InstanceScope(), new DefaultScope() };
}
boolean ignore = fPreferenceService.getBoolean(getPreferenceNodeQualifier(), HTMLCorePreferenceNames.IGNORE_ATTRIBUTE_NAMES, HTMLCorePreferenceNames.IGNORE_ATTRIBUTE_NAMES_DEFAULT, fLookupOrder);
String ignoreList = fPreferenceService.getString(getPreferenceNodeQualifier(), HTMLCorePreferenceNames.ATTRIBUTE_NAMES_TO_IGNORE, HTMLCorePreferenceNames.ATTRIBUTE_NAMES_TO_IGNORE_DEFAULT, fLookupOrder);
Set result = new HashSet();
if (ignoreList.trim().length() > 0) {
// $NON-NLS-1$
String[] names = ignoreList.split(",");
for (int i = 0; names != null && i < names.length; i++) {
String name = names[i] == null ? null : names[i].trim();
if (name != null && name.length() > 0)
result.add(name.toLowerCase());
}
}
String name = getAttributeName(node, offset);
if (name == null)
continue;
// If ignore == false. then show a quick fix anyway (due to allow to turn 'ignore' option on)
if (!ignore || shouldShowQuickFix(result, name.toLowerCase())) {
IgnoreAttributeNameCompletionProposal p = new IgnoreAttributeNameCompletionProposal(name.toLowerCase(), offset, NLS.bind(HTMLUIMessages.DoNotValidateAttribute, name), HTMLUIMessages.DoNotValidateAttributeAddInfo, node);
if (!proposals.contains(p))
proposals.add(p);
}
int dashIndex = name.indexOf('-');
while (dashIndex != -1) {
StringBuffer namePattern = new StringBuffer(name.substring(0, dashIndex + 1)).append('*');
// a more common pattern is already created
if (ignore && result.contains(namePattern.toString().toLowerCase()))
break;
IgnoreAttributeNameCompletionProposal p = new IgnoreAttributeNameCompletionProposal(namePattern.toString().toLowerCase(), offset, NLS.bind(HTMLUIMessages.DoNotValidateAllAttributes, namePattern.toString()), HTMLUIMessages.DoNotValidateAllAttributesAddInfo, node);
if (!proposals.contains(p))
proposals.add(p);
dashIndex = name.indexOf('-', dashIndex + 1);
}
}
}
}
if (proposals.isEmpty())
return null;
return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
}
use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.
the class TestStructuredTextEditor method testInitialReconciling.
/**
* Test receiving the initial reconcile notification when the editor opens
*/
public void testInitialReconciling() throws Exception {
IFile file = getOrCreateFile(PROJECT_NAME + "/" + "reconcilingtest.xml");
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
final int[] state = new int[2];
Arrays.fill(state, -1);
ISourceReconcilingListener listener = new ISourceReconcilingListener() {
int mod = 0;
public void reconciled(IDocument document, IAnnotationModel model, boolean forced, IProgressMonitor progressMonitor) {
state[1] = mod++;
}
public void aboutToBeReconciled() {
state[0] = mod++;
}
};
IEditorPart editor = IDE.openEditor(activePage, file, "org.eclipse.wst.sse.ui.StructuredTextEditor.test");
try {
assertTrue("Not a StructuredTextEditor", editor instanceof StructuredTextEditor);
addReconcilingListener((StructuredTextEditor) editor, listener);
waitForReconcile(state);
assertTrue("Initial: Reconciling did not complete in a timely fashion", state[0] != -1 && state[1] != -1);
assertTrue("Initial: aboutToBeReconciled not invoked first (" + state[0] + ")", state[0] == 0);
assertTrue("Initial: reconciled not invoked after aboutToBeReconciled (" + state[1] + ")", state[1] == 1);
} finally {
if (editor != null && activePage != null) {
activePage.closeEditor(editor, false);
}
}
}
use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.
the class TestStructuredTextEditor method testFocusedReconciling.
/**
* Test that an editor notifies reconciling listeners when the editor gets focus.
*/
public void testFocusedReconciling() throws Exception {
IFile file = getOrCreateFile(PROJECT_NAME + "/" + "focustest.xml");
IFile fileAlt = getOrCreateFile(PROJECT_NAME + "/" + "focustestAlt.xml");
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
final int[] state = new int[2];
Arrays.fill(state, -1);
ISourceReconcilingListener listener = new ISourceReconcilingListener() {
int mod = 0;
public void reconciled(IDocument document, IAnnotationModel model, boolean forced, IProgressMonitor progressMonitor) {
state[1] = mod++;
}
public void aboutToBeReconciled() {
state[0] = mod++;
}
};
IEditorPart editor = IDE.openEditor(activePage, file, "org.eclipse.wst.sse.ui.StructuredTextEditor.test");
try {
assertTrue("Not a StructuredTextEditor", editor instanceof StructuredTextEditor);
addReconcilingListener((StructuredTextEditor) editor, listener);
waitForReconcile(state);
assertTrue("Initial: Reconciling did not complete in a timely fashion", state[0] != -1 && state[1] != -1);
assertTrue("Initial: aboutToBeReconciled not invoked first (" + state[0] + ")", state[0] == 0);
assertTrue("Initial: reconciled not invoked after aboutToBeReconciled (" + state[1] + ")", state[1] == 1);
IDE.openEditor(activePage, fileAlt, "org.eclipse.wst.sse.ui.StructuredTextEditor.test");
Arrays.fill(state, -1);
IEditorPart editorPart = IDE.openEditor(activePage, file, "org.eclipse.wst.sse.ui.StructuredTextEditor.test");
assertEquals("Didn't get the original editor back.", editor, editorPart);
waitForReconcile(state);
assertTrue("Modified: Reconciling did not complete in a timely fashion", state[0] != -1 && state[1] != -1);
assertTrue("Modified: aboutToBeReconciled not invoked first (" + state[0] + ")", state[0] == 2);
assertTrue("Modified: reconciled not invoked after aboutToBeReconciled (" + state[1] + ")", state[1] == 3);
} finally {
if (editor != null && activePage != null) {
activePage.closeEditor(editor, false);
}
}
}
Aggregations