~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/SpecfileHover.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2007, 2009 Red Hat, Inc.
 
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
 *    Red Hat - initial API and implementation
 
10
 *    Alphonse Van Assche
 
11
 *******************************************************************************/
 
12
 
 
13
package org.eclipse.linuxtools.internal.rpm.ui.editor;
 
14
 
 
15
import java.util.regex.Matcher;
 
16
import java.util.regex.Pattern;
 
17
 
 
18
import org.eclipse.jface.text.BadLocationException;
 
19
import org.eclipse.jface.text.DefaultInformationControl;
 
20
import org.eclipse.jface.text.IDocument;
 
21
import org.eclipse.jface.text.IInformationControl;
 
22
import org.eclipse.jface.text.IInformationControlCreator;
 
23
import org.eclipse.jface.text.IRegion;
 
24
import org.eclipse.jface.text.ITextHover;
 
25
import org.eclipse.jface.text.ITextHoverExtension;
 
26
import org.eclipse.jface.text.ITextViewer;
 
27
import org.eclipse.jface.text.Region;
 
28
import org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource;
 
29
import org.eclipse.linuxtools.internal.rpm.ui.editor.preferences.PreferenceConstants;
 
30
import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor;
 
31
import org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile;
 
32
import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine;
 
33
import org.eclipse.swt.graphics.Point;
 
34
import org.eclipse.swt.widgets.Shell;
 
35
 
 
36
public class SpecfileHover implements ITextHover, ITextHoverExtension {
 
37
 
 
38
        private static final String EMPTY_STRING = ""; //$NON-NLS-1$
 
39
        private SpecfileEditor editor;
 
40
 
 
41
        public SpecfileHover(SpecfileEditor editor) {
 
42
                this.editor = editor;
 
43
        }
 
44
 
 
45
        public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
 
46
                if (hoverRegion == null || hoverRegion.getLength() == 0)
 
47
                        return null;
 
48
                
 
49
                Specfile spec = editor.getSpecfile();
 
50
                
 
51
                
 
52
                String currentSelection;
 
53
                try {
 
54
                        currentSelection = textViewer.getDocument().get(hoverRegion.getOffset() + 1, hoverRegion.getLength() - 1);
 
55
                } catch (BadLocationException e) {
 
56
                        return null;
 
57
                }
 
58
                
 
59
                
 
60
        // First we try to get a define based on the given name
 
61
                SpecfileDefine define = spec.getDefine(currentSelection);
 
62
                
 
63
        String value = currentSelection + ": "; //$NON-NLS-1$
 
64
                
 
65
                if (define != null) {
 
66
                    value += define.getStringValue();
 
67
                    return value;
 
68
                }
 
69
                
 
70
                String macroLower = currentSelection.toLowerCase();
 
71
 
 
72
                // If there's no such define we try to see if it corresponds to
 
73
                // a Source or Patch declaration
 
74
                String retrivedValue = getSourceOrPatchValue(spec, macroLower);
 
75
                if (retrivedValue != null) 
 
76
                        return value += retrivedValue;
 
77
                else {
 
78
                        // If it does not correspond to a Patch or Source macro, try to find it
 
79
                        // in the macro proposals list.
 
80
                        retrivedValue = getMacroValueFromMacroList(currentSelection);
 
81
                        if (retrivedValue != null) 
 
82
                                return value += retrivedValue;
 
83
                        else {
 
84
                                // If it does not correspond to a macro in the list, try to find it
 
85
                                // in the RPM list. 
 
86
                                retrivedValue = Activator.getDefault().getRpmPackageList().getValue(currentSelection.replaceFirst(":",EMPTY_STRING)); //$NON-NLS-1$ 
 
87
                                if (retrivedValue != null)
 
88
                                        return retrivedValue;
 
89
                        }
 
90
                }
 
91
       // We return null in other cases, so we don't show hover information
 
92
       // for unrecognized macros and RPM packages.
 
93
       return null;
 
94
        }
 
95
 
 
96
        public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
 
97
                
 
98
                if (textViewer != null) {
 
99
                        /*
 
100
                         * If the hover offset falls within the selection range return the
 
101
                         * region for the whole selection.
 
102
                         */
 
103
                        Point selectedRange = textViewer.getSelectedRange();
 
104
                        if (selectedRange.x >= 0 && selectedRange.y      > 0
 
105
                                        && offset >= selectedRange.x
 
106
                                        && offset <= selectedRange.x + selectedRange.y)
 
107
                                return new Region(selectedRange.x, selectedRange.y);
 
108
                        else {
 
109
                                IRegion region = findWord(textViewer.getDocument(), offset);
 
110
                                if (region.equals(new Region(offset, 0))) {
 
111
                                        region = findPackages(textViewer.getDocument(), offset);
 
112
                                }
 
113
                                return region;
 
114
                        }
 
115
                }
 
116
                return null;
 
117
        }
 
118
 
 
119
        public IInformationControlCreator getHoverControlCreator() {
 
120
                return new IInformationControlCreator() {
 
121
                        public IInformationControl createInformationControl(Shell parent) {
 
122
                                return new DefaultInformationControl(parent, false);
 
123
                        }
 
124
                };
 
125
        }
 
126
        
 
127
        
 
128
        public static IRegion findWord(IDocument document, int offset) {
 
129
                int start = -1;
 
130
                int end = -1;
 
131
                boolean beginsWithBrace = false;
 
132
 
 
133
                try {
 
134
                        int pos = offset;
 
135
                        char c;
 
136
 
 
137
                        while (pos >= 0) {
 
138
                                c = document.getChar(pos);
 
139
                                if (c == '%') {
 
140
                                        if (document.getChar(pos + 1) == '{')
 
141
                                                beginsWithBrace = true;
 
142
                                        break;
 
143
                                }
 
144
                                else if (c == '\n' || c == '}'){
 
145
                                        // if we hit the beginning of the line, it's not a macro
 
146
                                        return new Region(offset, 0);
 
147
                                }
 
148
                                --pos;
 
149
                        }
 
150
                        
 
151
                        if (!beginsWithBrace)
 
152
                                --pos;
 
153
 
 
154
                        start = pos;
 
155
 
 
156
                        pos = offset;
 
157
                        int length = document.getLength();
 
158
 
 
159
                        while (pos < length) {
 
160
                                c = document.getChar(pos);
 
161
                                if (beginsWithBrace && (c == '}')) {
 
162
                                        break;
 
163
                                }
 
164
                                else if (c == '\n' || c == '%' || c == '('){ // '(' is needed for the %deffatt( case
 
165
                                        break;
 
166
//                                      Do not return empty region here. We have a work.
 
167
//                                      return new Region(offset, 0);
 
168
                                } else if (!beginsWithBrace && c == ' ') {
 
169
                                        break;
 
170
                                }
 
171
                                ++pos;
 
172
                        }
 
173
 
 
174
                        end = pos;
 
175
 
 
176
                } catch (BadLocationException x) {
 
177
                }
 
178
 
 
179
                if (start >= -1 && end > -1) {
 
180
                        if (start == offset)
 
181
                                return new Region(start, end - start);
 
182
                        else
 
183
                                return new Region(start + 1, end - start - 1);
 
184
                }
 
185
 
 
186
                return null;
 
187
        }
 
188
        
 
189
        public static IRegion findPackages(IDocument document, int offset) {
 
190
                int start = -1;
 
191
                int end = -1;
 
192
                boolean beginsWithSpace = false;
 
193
                try {
 
194
                        int pos = offset;
 
195
                        char c;
 
196
                        while (pos >= 0) {
 
197
                                c = document.getChar(pos);
 
198
                                if (c == ' ' || c =='\t' || c==':')  {
 
199
                                        if (Character.isLetter(document.getChar(pos + 1))) {
 
200
                                                beginsWithSpace = true;
 
201
                                                break;
 
202
                                        } else if (c == '\n'){
 
203
                                                return new Region(offset, 0);
 
204
                                        }
 
205
                                }  
 
206
                                --pos;
 
207
                        }
 
208
                        --pos;
 
209
                        start = pos;
 
210
                        pos = offset;
 
211
                        int length = document.getLength();
 
212
                        while (pos < length) {
 
213
                                c = document.getChar(pos);
 
214
                                if (beginsWithSpace && (!Character.isLetter(c) && !Character.isDigit(c) && c != '-')) {
 
215
                                        break;
 
216
                                }
 
217
                                else if (c == '\n'){
 
218
                                        return new Region(offset, 0);
 
219
                                }
 
220
                                ++pos;
 
221
                        }
 
222
                        end = pos;
 
223
 
 
224
                } catch (BadLocationException x) {
 
225
                }
 
226
 
 
227
                if (start > -1 && end > -1) {
 
228
                        return new Region(start, end - start);
 
229
                }
 
230
                return null;
 
231
        }
 
232
        
 
233
        public static String getSourceOrPatchValue(Specfile spec, String patchOrSourceName) {
 
234
                String value = null;
 
235
                Pattern p = Pattern.compile("(source|patch)(\\d*)"); //$NON-NLS-1$
 
236
                Matcher m = p.matcher(patchOrSourceName);
 
237
 
 
238
                if (m.matches()) {
 
239
                        String digits = m.group(2);
 
240
 
 
241
                        SpecfileSource source = null;
 
242
                        int number = -1;
 
243
 
 
244
                        if (digits != null && digits.equals(EMPTY_STRING)) {
 
245
                                number = 0;
 
246
                        } else if (digits != null && !digits.equals(EMPTY_STRING)) {
 
247
                                number = Integer.parseInt(digits);
 
248
                        }
 
249
 
 
250
                        if (number != -1) {
 
251
                                if (m.group(1).equals("source")) //$NON-NLS-1$
 
252
                                        source = spec.getSource(number);
 
253
                                else if (m.group(1).equals("patch")) //$NON-NLS-1$
 
254
                                        source = spec.getPatch(number);
 
255
 
 
256
                                if (source != null) {
 
257
                                        value = source.getFileName();
 
258
                                }
 
259
                        }
 
260
                }
 
261
                return value;
 
262
        }
 
263
 
 
264
        public static String getMacroValueFromMacroList(String macroName) {
 
265
                String value = null;
 
266
                if (Activator.getDefault().getRpmMacroList().findKey("%" + macroName)) { //$NON-NLS-1$
 
267
                        String currentConfig = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.P_MACRO_HOVER_CONTENT);
 
268
                        // Show content of the macro according with the configuration set
 
269
                        // in the macro preference page.
 
270
                        if (currentConfig.equals(PreferenceConstants.P_MACRO_HOVER_CONTENT_VIEWDESCRIPTION))
 
271
                                value = Activator.getDefault().getRpmMacroList().getValue(macroName);
 
272
                        else
 
273
                                value = RpmMacroProposalsList.getMacroEval("%" + macroName); //$NON-NLS-1$
 
274
                }
 
275
                return value;
 
276
        }
 
277
}