1 | /******************************************************************************* |
---|
2 | * Copyright (c) 2005, 2006 IBM Corporation and others. |
---|
3 | * All rights reserved. This program and the accompanying materials |
---|
4 | * are made available under the terms of the Eclipse Public License v1.0 |
---|
5 | * which accompanies this distribution, and is available at |
---|
6 | * http://www.eclipse.org/legal/epl-v10.html |
---|
7 | * |
---|
8 | * Contributors: |
---|
9 | * IBM Corporation - initial API and implementation |
---|
10 | *******************************************************************************/ |
---|
11 | package org.modelica.mdt.ui.hover; |
---|
12 | |
---|
13 | import org.eclipse.core.runtime.Assert; |
---|
14 | |
---|
15 | import org.eclipse.jface.text.BadLocationException; |
---|
16 | import org.eclipse.jface.text.IDocument; |
---|
17 | import org.eclipse.jface.text.ITextViewer; |
---|
18 | |
---|
19 | /** |
---|
20 | * Describes the context of an invocation of content assist in a text viewer. The context knows the |
---|
21 | * document, the invocation offset and can lazily compute the identifier prefix preceding the |
---|
22 | * invocation offset. It may know the viewer. |
---|
23 | * <p> |
---|
24 | * Subclasses may add information to their environment. For example, source code editors may provide |
---|
25 | * specific context information such as an AST. |
---|
26 | * </p> |
---|
27 | * <p> |
---|
28 | * Clients may instantiate and subclass. |
---|
29 | * </p> |
---|
30 | * |
---|
31 | * @since 3.2 |
---|
32 | */ |
---|
33 | public class ContentAssistInvocationContext { |
---|
34 | |
---|
35 | /* state */ |
---|
36 | private final ITextViewer fViewer; |
---|
37 | private final IDocument fDocument; |
---|
38 | private final int fOffset; |
---|
39 | |
---|
40 | /* cached additional info */ |
---|
41 | private CharSequence fPrefix; |
---|
42 | |
---|
43 | /** |
---|
44 | * Equivalent to |
---|
45 | * {@linkplain #ContentAssistInvocationContext(ITextViewer, int) ContentAssistInvocationContext(viewer, viewer.getSelectedRange().x)}. |
---|
46 | * |
---|
47 | * @param viewer the text viewer that content assist is invoked in |
---|
48 | */ |
---|
49 | public ContentAssistInvocationContext(ITextViewer viewer) { |
---|
50 | this(viewer, viewer.getSelectedRange().x); |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Creates a new context for the given viewer and offset. |
---|
55 | * |
---|
56 | * @param viewer the text viewer that content assist is invoked in |
---|
57 | * @param offset the offset into the viewer's document where content assist is invoked at |
---|
58 | */ |
---|
59 | public ContentAssistInvocationContext(ITextViewer viewer, int offset) { |
---|
60 | Assert.isNotNull(viewer); |
---|
61 | fViewer= viewer; |
---|
62 | fDocument= null; |
---|
63 | fOffset= offset; |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Creates a new context with no viewer or invocation offset set. |
---|
68 | */ |
---|
69 | protected ContentAssistInvocationContext() { |
---|
70 | fDocument= null; |
---|
71 | fViewer= null; |
---|
72 | fOffset= -1; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Creates a new context for the given document and offset. |
---|
77 | * |
---|
78 | * @param document the document that content assist is invoked in |
---|
79 | * @param offset the offset into the document where content assist is invoked at |
---|
80 | */ |
---|
81 | public ContentAssistInvocationContext(IDocument document, int offset) { |
---|
82 | Assert.isNotNull(document); |
---|
83 | Assert.isTrue(offset >= 0); |
---|
84 | fViewer= null; |
---|
85 | fDocument= document; |
---|
86 | fOffset= offset; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Returns the invocation offset. |
---|
91 | * |
---|
92 | * @return the invocation offset |
---|
93 | */ |
---|
94 | public final int getInvocationOffset() { |
---|
95 | return fOffset; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Returns the viewer, <code>null</code> if not available. |
---|
100 | * |
---|
101 | * @return the viewer, possibly <code>null</code> |
---|
102 | */ |
---|
103 | public final ITextViewer getViewer() { |
---|
104 | return fViewer; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Returns the document that content assist is invoked on, or <code>null</code> if not known. |
---|
109 | * |
---|
110 | * @return the document or <code>null</code> |
---|
111 | */ |
---|
112 | public IDocument getDocument() { |
---|
113 | if (fDocument == null) { |
---|
114 | if (fViewer == null) |
---|
115 | return null; |
---|
116 | return fViewer.getDocument(); |
---|
117 | } |
---|
118 | return fDocument; |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Computes the identifier (as specified by {@link Character#isJavaIdentifierPart(char)}) that |
---|
123 | * immediately precedes the invocation offset. |
---|
124 | * |
---|
125 | * @return the prefix preceding the content assist invocation offset, <code>null</code> if |
---|
126 | * there is no document |
---|
127 | * @throws BadLocationException if accessing the document fails |
---|
128 | */ |
---|
129 | public CharSequence computeIdentifierPrefix() throws BadLocationException { |
---|
130 | if (fPrefix == null) { |
---|
131 | IDocument document= getDocument(); |
---|
132 | if (document == null) |
---|
133 | return null; |
---|
134 | int end= getInvocationOffset(); |
---|
135 | int start= end; |
---|
136 | while (--start >= 0) { |
---|
137 | if (!Character.isJavaIdentifierPart(document.getChar(start))) |
---|
138 | break; |
---|
139 | } |
---|
140 | start++; |
---|
141 | fPrefix= document.get(start, end - start); |
---|
142 | } |
---|
143 | |
---|
144 | return fPrefix; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Invocation contexts are equal if they describe the same context and are of the same type. |
---|
149 | * This implementation checks for <code>null</code> values and class equality. Subclasses |
---|
150 | * should extend this method by adding checks for their context relevant fields (but not |
---|
151 | * necessarily cached values). |
---|
152 | * <p> |
---|
153 | * Example: |
---|
154 | * |
---|
155 | * <pre> |
---|
156 | * class MyContext extends ContentAssistInvocationContext { |
---|
157 | * private final Object fState; |
---|
158 | * private Object fCachedInfo; |
---|
159 | * |
---|
160 | * ... |
---|
161 | * |
---|
162 | * public boolean equals(Object obj) { |
---|
163 | * if (!super.equals(obj)) |
---|
164 | * return false; |
---|
165 | * MyContext other= (MyContext) obj; |
---|
166 | * return fState.equals(other.fState); |
---|
167 | * } |
---|
168 | * } |
---|
169 | * </pre> |
---|
170 | * |
---|
171 | * </p> |
---|
172 | * <p> |
---|
173 | * Subclasses should also extend {@link Object#hashCode()}. |
---|
174 | * </p> |
---|
175 | * |
---|
176 | * @param obj {@inheritDoc} |
---|
177 | * @return {@inheritDoc} |
---|
178 | */ |
---|
179 | public boolean equals(Object obj) { |
---|
180 | if (obj == null) |
---|
181 | return false; |
---|
182 | if (!getClass().equals(obj.getClass())) |
---|
183 | return false; |
---|
184 | ContentAssistInvocationContext other= (ContentAssistInvocationContext) obj; |
---|
185 | return (fViewer == null && other.fViewer == null || fViewer != null && fViewer.equals(other.fViewer)) && fOffset == other.fOffset && (fDocument == null && other.fDocument == null || fDocument != null && fDocument.equals(other.fDocument)); |
---|
186 | } |
---|
187 | |
---|
188 | /* |
---|
189 | * @see java.lang.Object#hashCode() |
---|
190 | */ |
---|
191 | public int hashCode() { |
---|
192 | return 23459213 << 5 | (fViewer == null ? 0 : fViewer.hashCode() << 3) | fOffset; |
---|
193 | } |
---|
194 | } |
---|