Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2d039c1f965a0771a1619a5c61ad74a555ea4dd2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*******************************************************************************
 * Copyright (c) 2007 Alphonse Van Assche.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Alphonse Van Assche - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.rpm.ui.editor;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.viewers.IPostSelectionProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile;
import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine;

class RpmMacroOccurrencesUpdater implements ISelectionChangedListener {
												   
	private static final String ANNOTATION_TYPE = Activator.PLUGIN_ID + ".highlightannotation"; //$NON-NLS-1$

	private final SpecfileEditor fEditor;

	private final List<Annotation> fOldAnnotations = new LinkedList<Annotation>();

	/**
	 * Creates a new instance on editor <code>specfileEditor</code>.
	 * 
	 * @param specfileEditor The editor to mark occurrences on.
	 */
	public RpmMacroOccurrencesUpdater(SpecfileEditor specfileEditor) {
		((IPostSelectionProvider) specfileEditor.getSelectionProvider())
				.addPostSelectionChangedListener(this);
		fEditor = specfileEditor;
	}

	/*
	 * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
	 */
	public void selectionChanged(SelectionChangedEvent event) {
		update((ISourceViewer) event.getSource());
	}

	/**
	 * Updates the drawn annotations.
	 * 
	 * @param viewer The viewer to get the document and annotation model from
	 */
	public void update(ISourceViewer viewer) {
		try {
			IDocument document = viewer.getDocument();
			IAnnotationModel model = viewer.getAnnotationModel();
			if (document == null || model == null) {
				return;
			}
			removeOldAnnotations(model);
			String currentSelectedWord = getWordAtSelection(fEditor.getSelectionProvider()
					.getSelection(), document);
			if (isMacro(currentSelectedWord)) {
				Specfile spec = fEditor.getSpecfile();
				SpecfileDefine define = spec.getDefine(currentSelectedWord);
				String word = currentSelectedWord + ": "; //$NON-NLS-1$
                if (define != null) {
                	word += define.getStringValue();
                } else {
              		// If there's no such define we try to see if it corresponds to
            		// a Source or Patch declaration
            		String retrivedValue = SpecfileHover.getSourceOrPatchValue(spec, currentSelectedWord.toLowerCase());
            		if (retrivedValue != null) 
            			word += retrivedValue;
            		else {
            			// If it does not correspond to a Patch or Source macro, try to find it
            			// in the macro proposals list.
            			retrivedValue = SpecfileHover.getMacroValueFromMacroList(currentSelectedWord);
            			if (retrivedValue != null) 
            				word += retrivedValue;
            		}
                }
                createNewAnnotations(currentSelectedWord, word, document, model);
			}
		} catch (BadLocationException e) {
			SpecfileLog.logError(e);
		}
	}

	/**
	 * Removes the previous set of annotations from the annotation model.
	 * 
	 * @param model
	 *            the annotation model
	 */
	private void removeOldAnnotations(IAnnotationModel model) {
		for (Annotation annotation: fOldAnnotations) {
			model.removeAnnotation(annotation);
		}
		fOldAnnotations.clear();
	}

	/**
	 * Checks if <code>word</code> is an macro.
	 * 
	 * @param word
	 *            the word to check
	 * 
	 * @return <code>true</code> if <code>word</code> is an macro,
	 *         <code>false</code> otherwise
	 */
	private boolean isMacro(String word) {
		List<SpecfileDefine> defines = getMacros();
		if (word.length() > 0) {
			for (SpecfileDefine define: defines) {
				if (containsWord(define, word)) {
					return true;
				}
			}
			if (Activator.getDefault().getRpmMacroList().getProposals(
					"%" + word).size() > 0) //$NON-NLS-1$
				return true;
		}
		return false;
	}

	/**
	 * Retrieves the macros from the editor's specfile.
	 * 
	 * @return the macros from the editor's specfile
	 */
	private List<SpecfileDefine> getMacros() {
		Specfile specfile = fEditor.getSpecfile();
		if (specfile != null) {
			List<SpecfileDefine> macros = specfile.getDefines();
			if (macros != null)
				return macros;
		}
		return new ArrayList<SpecfileDefine>();
	}

	/**
	 * Returns <code>true</code> if <code>macro</code> equals the word
	 * <code>current</code>.
	 * 
	 * @param macro
	 *            the <code>macro</code> to check
	 * @param current
	 *            the word to look for
	 * 
	 * @return <code>true</code> if <code>macro</code> contains the word
	 *         <code>current</code>,<code>false</code> if not
	 */
	private boolean containsWord(SpecfileDefine macro, String current) {
		return macro.getName().toLowerCase().equals(current);
	}

	/**
	 * Returns the word at the current selection / caret position.
	 * 
	 * @param selection
	 *            the selection
	 * @param document
	 *            the document
	 * @return the currently selected text, or the word at the caret if the
	 *         selection has length 0
	 * @throws BadLocationException
	 *             if accessing the document fails
	 */
	private String getWordAtSelection(ISelection selection, IDocument document)
			throws BadLocationException {
		String word;
		if (selection instanceof ITextSelection) {
			ITextSelection ts = (ITextSelection) selection;
			int offset = ts.getOffset();
			int end = offset + ts.getLength();

			// non-empty selections
			if (end != offset) {
				word = ts.getText();
			} else {
				while (offset > 0 && isDefineChar(document.getChar(offset - 1)))
					offset--;
				while (end < document.getLength()
						&& isDefineChar(document.getChar(end)))
					end++;

				word = document.get(offset, end - offset);
			}
		} else {
			word = ""; //$NON-NLS-1$
		}
		return word.toLowerCase();
	}

	private boolean isDefineChar(char c) {
		return c != '{' && c != '}' && c != '?' && !Character.isWhitespace(c);
	}

	/**
	 * Adds an annotation for every occurrence of <code>macro</code> in the
	 * document. Also stores the created annotations in
	 * <code>fOldAnnotations</code>.
	 * 
	 * @param macro
	 *            the word to look for
	 * @param document
	 *            the document
	 * @param model
	 *            the annotation model
	 */
	private void createNewAnnotations(String macro, String hoverContent, IDocument document,
			IAnnotationModel model) {
		String content = document.get().toLowerCase();
		int idx = content.indexOf(macro.toLowerCase());
		while (idx != -1) {
			Annotation annotation = new Annotation(ANNOTATION_TYPE, false,
					hoverContent);
			Position position = new Position(idx, macro.length());
			model.addAnnotation(annotation, position);
			fOldAnnotations.add(annotation);
			idx = content.indexOf(macro, idx + 1);
		}
	}

	public void dispose() {
		((IPostSelectionProvider) fEditor.getSelectionProvider())
				.removePostSelectionChangedListener(this);
	}
}

Back to the top