summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java')
-rw-r--r--plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java127
1 files changed, 127 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java
new file mode 100644
index 0000000..5a90fca
--- /dev/null
+++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/editors/bitbake/BBVariableTextHover.java
@@ -0,0 +1,127 @@
1/*******************************************************************************
2 * Copyright (c) 2013 Intel Corporation.
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 * Intel - initial API and implementation
10 *******************************************************************************/
11package org.yocto.bc.ui.editors.bitbake;
12
13import java.net.URI;
14import java.util.Map;
15
16import org.eclipse.core.runtime.IProgressMonitor;
17import org.eclipse.core.runtime.IStatus;
18import org.eclipse.core.runtime.Status;
19import org.eclipse.core.runtime.jobs.Job;
20import org.eclipse.jface.text.IRegion;
21import org.eclipse.jface.text.ITextHover;
22import org.eclipse.jface.text.ITextViewer;
23import org.eclipse.jface.text.Region;
24import org.yocto.bc.bitbake.BBRecipe;
25import org.yocto.bc.bitbake.BBSession;
26import org.yocto.bc.ui.Activator;
27
28/**
29 * Maps BB Variables in the editor to BBSession
30 * @author kgilmer
31 *
32 */
33class BBVariableTextHover implements ITextHover {
34 private final BBSession session;
35 private volatile Map<String, String> envMap;
36
37 public BBVariableTextHover(BBSession session, URI file) {
38 this.session = session;
39 envMap = getEnvironmentMap();
40 LoadRecipeJob loadRecipeJob = new LoadRecipeJob(getFilename(file), file);
41 loadRecipeJob.schedule();
42 }
43
44 private Map<String, String> getEnvironmentMap() {
45 if (envMap == null)
46 envMap = this.session.getProperties();
47 return envMap;
48 }
49
50 private String getFilename(URI uri) {
51 return uri.getPath();
52 }
53
54 public IRegion getHoverRegion(ITextViewer tv, int off) {
55 return new Region(off, 0);
56 }
57
58 public String getHoverInfo(ITextViewer tv, IRegion r) {
59 try {
60 IRegion lineRegion = tv.getDocument().getLineInformationOfOffset(r.getOffset());
61
62 return getBBVariable(tv.getDocument().get(lineRegion.getOffset(), lineRegion.getLength()).toCharArray(), r.getOffset() - lineRegion.getOffset());
63 } catch (Exception e) {
64 return "";
65 }
66 }
67
68 private String getBBVariable(char[] line, int offset) {
69 // Find start of word.
70 int i = offset;
71
72 while (line[i] != ' ' && line[i] != '$' && i > 0) {
73 i--;
74 }
75
76 if (i < 0 || line[i] != '$') {
77 return ""; //this is not a BB variable.
78 }
79
80 // find end of word
81 int start = i;
82 i = offset;
83
84 while (line[i] != ' ' && line[i] != '}' && i <= line.length) {
85 i++;
86 }
87
88 if (line[i] != '}') {
89 return ""; //this bb variable didn't terminate as expected
90 }
91
92 String key = new String(line, start + 2, i - start - 2);
93 String val = (String) getEnvironmentMap().get(key);
94
95 if (val == null) {
96 val = "";
97 }
98
99 if (val.length() > 64) {
100 val = val.substring(0, 64) + '\n' + val.substring(65);
101 }
102
103 return val;
104 }
105
106 private class LoadRecipeJob extends Job {
107 private final URI filePath;
108
109 public LoadRecipeJob(String name, URI filePath) {
110 super("Extracting BitBake environment for " + name);
111 this.filePath = filePath;
112 }
113
114 @Override
115 protected IStatus run(IProgressMonitor mon) {
116 try {
117 BBRecipe recipe = Activator.getBBRecipe(session, filePath);
118 recipe.initialize();
119 envMap = recipe;
120 } catch (Exception e) {
121 return new Status(IStatus.WARNING, Activator.PLUGIN_ID, "Unable to load session for " + filePath, e);
122 }
123
124 return Status.OK_STATUS;
125 }
126 }
127}