Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6c7c74cad1d57bf53dd0b083b91384da32753580 (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
/*******************************************************************************
 * Copyright (c) 2014 Jayant Gupta
 * 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:
 * 		Jayant Gupta (initial contribution)
 *
 *
 *******************************************************************************/

package org.eclipse.etrice.ui.behavior.actioneditor.sourceviewer;

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

import org.eclipse.etrice.ui.behavior.fsm.detailcode.IDetailExpressionProvider;
import org.eclipse.etrice.ui.behavior.fsm.detailcode.IDetailExpressionProvider.ExpressionFeature;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.swt.graphics.Point;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.lib.Pair;

/**
 * An Action Editor specific content assist processor. The processor lexically
 * analyzes the Action Code using {@link ActionCodeContext} and generates
 * proposals based on this context.
 * 
 * @author jayant
 */
public class ActionCodeAssistProcessor implements IContentAssistProcessor {

	/** the local reference of the associated configuration */
	private ActionCodeEditorConfiguration fConfiguration;

	private DetailExpressionUIProvider exprProvider;

	public ActionCodeAssistProcessor(ActionCodeEditorConfiguration configuration) {
		super();
		fConfiguration = configuration;
		exprProvider = fConfiguration.getDetailExpressionProvider();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @author jayant
	 */
	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();

		// get last context feature + prefix
		DetailExpressionAssistParser resolver = new DetailExpressionAssistParser(viewer.getDocument(), offset,
				exprProvider);
		ExpressionFeature contextFeature = resolver.resolveLatestCompleted();
		String idPrefix = "";
		try {
			idPrefix = resolver.computeIdentifierPrefix(offset);
		}
		catch (BadLocationException e) {
		}

		// cancel, if user expects proposals from context, but contextFeature
		// could not be resolved
		if (contextFeature == null && isExpressionSeparator(viewer, offset))
			return new ICompletionProposal[0];

		// create proposals
		Iterable<ExpressionFeature> features = (contextFeature != null) ? exprProvider.getContextFeaturesWithPrefix(
				contextFeature, idPrefix) : exprProvider.getInitialFeaturesWithPrefix(idPrefix);
		for (ExpressionFeature feature : features) {
			try {
				proposals.add(createPrefixCompletionProposal(feature, offset, idPrefix));
			}
			catch (Exception e) {
			}
		}

		return proposals.toArray(new ICompletionProposal[proposals.size()]);
	}

	private boolean isExpressionSeparator(ITextViewer viewer, int offset) {
		try {
			return viewer.getDocument().getChar(offset - 1) == IDetailExpressionProvider.SEPARATOR;
		}
		catch (BadLocationException e) {
		}
		return false;
	}

	private ICompletionProposal createPrefixCompletionProposal(ExpressionFeature feature, int offset, String prefix) {
		Pair<String, Point> completionPair = exprProvider.getCompletion(feature);
		String completion = completionPair.getKey();
		String displayString = exprProvider.getDisplayString(feature);
		if (Strings.isEmpty(displayString))
			displayString = completion;

		ICompletionProposal proposal = new CompletionProposal(completion, offset - prefix.length(), prefix.length(),
				completion.length(), exprProvider.getImage(feature), displayString, null, null);

		Point selection = completionPair.getValue();
		if (selection != null)
			selection.x += (offset - prefix.length());
		return new CustomCompletionProposal(proposal, selection);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @author jayant
	 */
	@Override
	public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
		return null;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @author jayant
	 */
	@Override
	public char[] getCompletionProposalAutoActivationCharacters() {
		return new char[] { IDetailExpressionProvider.SEPARATOR };
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @author jayant
	 */
	@Override
	public char[] getContextInformationAutoActivationCharacters() {
		return null;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @author jayant
	 */
	@Override
	public String getErrorMessage() {
		return null;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @author jayant
	 */
	@Override
	public IContextInformationValidator getContextInformationValidator() {
		return null;
	}

}

Back to the top