aboutsummaryrefslogtreecommitdiffstats
path: root/org.openembedded.bc.ui/src/org/openembedded/bc/ui/editors/bitbake/BBVariableTextHover.java
blob: d98c305ca77f114eb31d42efb696492cc189e84f (plain)
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
/**
 * 
 */
package org.openembedded.bc.ui.editors.bitbake;

import java.io.File;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.openembedded.bc.bitbake.BBRecipe;
import org.openembedded.bc.bitbake.BBSession;
import org.openembedded.bc.ui.Activator;


/**
 * Maps BB Variables in the editor to BBSession
 * @author kgilmer
 *
 */
class BBVariableTextHover implements ITextHover {
	private final BBSession session;
	private volatile Map envMap;

	public BBVariableTextHover(BBSession session, String file) {
		this.session = session;
		envMap = session;
		LoadRecipeJob loadRecipeJob = new LoadRecipeJob(getFilename(file), file);
		loadRecipeJob.schedule();
	}

	private String getFilename(String file) {
		
		String [] elems = file.split(File.separator);
		
		return elems[elems.length - 1];
	}

	public IRegion getHoverRegion(ITextViewer tv, int off) {
		return new Region(off, 0);
	}

	public String getHoverInfo(ITextViewer tv, IRegion r) {
		try {
			IRegion lineRegion = tv.getDocument().getLineInformationOfOffset(r.getOffset());

			return getBBVariable(tv.getDocument().get(lineRegion.getOffset(), lineRegion.getLength()).toCharArray(), r.getOffset() - lineRegion.getOffset());
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	private String getBBVariable(char[] line, int offset) {
		// Find start of word.
		int i = offset;
		
		while (line[i] != ' ' && line[i] != '$' && i > 0) {
			i--;
		}
		
		if (i < 0 || line[i] != '$') {
			return "";  //this is not a BB variable.
		}
		
		// find end of word
		int start = i;
		i = offset;
		
		while (line[i] != ' ' && line[i] != '}' && i <= line.length) {
			i++;
		}
		
		if (line[i] != '}') {
			return "";  //this bb variable didn't terminate as expected
		}
		
		String key = new String(line, start + 2, i - start - 2);
		String val = (String) envMap.get(key);
		
		if (val == null) {
			val = "";
		}
		
		if (val.length() > 64) {
			val = val.substring(0, 64) + '\n' + val.substring(65);
		}
		
		return val;
	}
	
	private class LoadRecipeJob extends Job {
		private final String filePath;

		public LoadRecipeJob(String name, String filePath) {
			super("Extracting BitBake environment for " + name);
			this.filePath = filePath;
		}

		@Override
		protected IStatus run(IProgressMonitor mon) {
			try {
				BBRecipe recipe = new BBRecipe(session, filePath);
				recipe.initialize();
				envMap = recipe;
			} catch (Exception e) {
				return new Status(IStatus.WARNING, Activator.PLUGIN_ID, "Unable to load session for " + filePath, e);
			} 
			
			return Status.OK_STATUS;
		}
	}
}