Skip to main content
summaryrefslogtreecommitdiffstats
blob: 60e4aba9ccffc68f4dda3454c2a765ff61338928 (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
/*******************************************************************************
 * Copyright (c) 2005, 2006 committers of openArchitectureWare and others.
 * 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/
package org.eclipse.xtend.shared.ui.expression.editor.codeassist;

import java.util.List;
import java.util.Set;

import org.eclipse.internal.xtend.expression.codeassist.ProposalFactory;
import org.eclipse.internal.xtend.xtend.ast.Extension;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.swt.graphics.Image;
import org.eclipse.xpand2.XpandUtil;
import org.eclipse.xtend.expression.TypeNameUtil;
import org.eclipse.xtend.shared.ui.expression.editor.EditorImages;
import org.eclipse.xtend.typesystem.Operation;
import org.eclipse.xtend.typesystem.ParameterizedType;
import org.eclipse.xtend.typesystem.Property;
import org.eclipse.xtend.typesystem.StaticProperty;
import org.eclipse.xtend.typesystem.Type;

public class ProposalFactoryEclipseImpl implements ProposalFactory {

	public int offset;

	public ProposalFactoryEclipseImpl(final int offset) {
		this.offset = offset;
	}

	public Object createCollectionSpecificOperationProposal(final String insertString, final String displayString,
			final String prefix, final int cursor, final int marked) {
		final String displayStr = displayString;
		final String insertStr = insertString;
		final Image img = EditorImages.getImage(EditorImages.OPERATION);
		return new TextSelectingProposal(insertStr, offset - prefix.length(), prefix.length(), cursor, marked, img,
				displayStr, null, null);
	}

	public Object createPropertyProposal(final Property p, final String prefix, final boolean onOperation) {
		final String returnType = computeReturnType(p.getReturnType(), onOperation);
		final String displayStr = p.getName() + " " + returnType + " - "
				+ TypeNameUtil.getSimpleName(p.getOwner().getName());
		final String insertStr = p.getName();
		final Image img = EditorImages.getImage(EditorImages.PROPERTY);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), insertStr.length(), img,
				displayStr, null, null);
	}

	/**
	 * @see ProposalFactory#createStaticPropertyProposal(StaticProperty, String,
	 *      boolean)
	 */
	public Object createStaticPropertyProposal(final StaticProperty p, final String prefix, final boolean onOperation) {
		final String returnType = computeReturnType(p.getReturnType(), onOperation);
		final String displayStr = p.getName() + " " + returnType + " - "
				+ TypeNameUtil.getSimpleName(p.getOwner().getName());
		final String insertStr = p.getName();
		final Image img = EditorImages.getImage(EditorImages.STATICPROPERTY);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), insertStr.length(), img,
				displayStr, null, null);
	}

	private String computeReturnType(Type returnType, final boolean onOperation) {
		if (returnType == null)
			return "unknown";
		if (onOperation) {
			if (returnType instanceof ParameterizedType) {
				returnType = ((ParameterizedType) returnType).getInnerType();
			}
			return "List[" + XpandUtil.getLastSegment(returnType.getName()) + "]";
		}

		return TypeNameUtil.getSimpleName(returnType.getName());
	}

	public Object createOperationProposal(final Operation p, final String prefix, final boolean onOperation) {
		final String returnType = computeReturnType(p.getReturnType(), onOperation);
		final String displayStr = p.getName() + toParamString(p.getParameterTypes()) + " " + returnType + " - "
				+ TypeNameUtil.getSimpleName(p.getOwner().getName());
		final String insertStr = p.getName() + "()";
		int x = insertStr.length();
		if (p.getParameterTypes().size() > 0) {
			x--;
		}
		final Image img = EditorImages.getImage(EditorImages.OPERATION);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), x, img, displayStr, null,
				null);
	}

	public Object createExtensionProposal(final Extension p, final String prefix) {
		final String displayStr = p.getName() + toParamString(p, false);
		final String insertStr = p.getName() + "()";
		int x = insertStr.length();
		if (p.getFormalParameters().size() > 0) {
			x--;
		}
		final Image img = EditorImages.getImage(EditorImages.EXTENSION);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), x, img, displayStr, null,
				null);
	}

	public Object createExtensionOnMemberPositionProposal(final Extension p, final String prefix,
			final boolean onOperation) {
		final String displayStr = p.getName() + toParamString(p, true) + " - "
				+ (p.getFormalParameters().get(0)).getType();
		final String insertStr = p.getName() + "()";
		int x = insertStr.length();
		if (p.getFormalParameters().size() > 1) {
			x--;
		}
		final Image img = EditorImages.getImage(EditorImages.EXTENSION);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), x, img, displayStr, null,
				null);
	}

	private String toParamString(final List<Type> parameterTypes) {
		final StringBuffer b = new StringBuffer("(");
		for (int i = 0, x = parameterTypes.size(); i < x; i++) {
			b.append(TypeNameUtil.getSimpleName(parameterTypes.get(i).getName()));
			if (i + 1 < x) {
				b.append(",");
			}
		}
		b.append(")");
		return b.toString();
	}

	private String toParamString(final Extension p, final boolean member) {
		final StringBuffer b = new StringBuffer("(");
		int i = member ? 1 : 0;
		for (final int x = p.getFormalParameters().size(); i < x; i++) {
			b.append((p.getFormalParameters().get(i)).toString());
			if (i + 1 < x) {
				b.append(",");
			}
		}
		b.append(")");
		return b.toString();
	}

	public Object createVariableProposal(final String name, final Type t, final String prefix) {
		final String displayStr = name + " " + computeReturnType(t, false);
		final String insertStr = name;
		final Image img = EditorImages.getImage(EditorImages.VARIABLE);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), insertStr.length(), img,
				displayStr, null, null);
	}

	public Object createTypeProposal(final String insertString, final Type type, final String prefix) {
		String displayStr = TypeNameUtil.getSimpleName(type.getName());
		final String packName = TypeNameUtil.withoutLastSegment(type.getName());
		if (packName != null) {
			displayStr += " - " + packName;
		}

		final String insertStr = insertString;
		final Image img = EditorImages.getImage(EditorImages.TYPE);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), insertStr.length(), img,
				displayStr, null, null);
	}

	public Object createExtensionImportProposal(final String insertStr, final String displayStr, final String prefix,
			final int cursor, final int marked) {
		final Image img = EditorImages.getImage(EditorImages.EXT_IMPORT);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), insertStr.length(), img,
				displayStr, null, null);
	}

	public Object createNamespaceProposal(final String insertStr, final String displayStr, final String prefix) {
		final Image img = EditorImages.getImage(EditorImages.PACKAGE);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), insertStr.length(), img,
				displayStr, null, null);
	}

	public Object createDefinitionProposal(final String insertStr, final String displayStr, final String prefix) {
		final Image img = EditorImages.getImage(EditorImages.XPAND_DEFINE);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), insertStr.length(), img,
				displayStr, null, null);
	}

	public Object createStatementProposal(final String insertStr, final String displayStr, final String prefix,
			final int cursor, final int marked) {
		final Image img = EditorImages.getImage(EditorImages.EXT_IMPORT);
		return new CompletionProposal(insertStr, offset - prefix.length(), prefix.length(), insertStr.length(), img,
				displayStr, null, null);
	}

	public Object createStatementProposal(final String insertString, final String displayString, final String prefix) {
		throw new UnsupportedOperationException();
	}

	public Object createKeywordProposal(final String insertString, final String displayString, final String prefix) {
		throw new UnsupportedOperationException();
	}

	public boolean isDuplicate(Set<String> nameCache, Object proposal) {
		if (nameCache == null || proposal == null)
			throw new IllegalArgumentException();

		CompletionProposal p = castToProposal(proposal);
		if (p != null) {
			if (nameCache.contains(p.getDisplayString()))
				return true;
			else
				return false;
		}
		return true;
	}

	public void addToCache(Set<String> nameCache, Object proposal) {
		CompletionProposal p = castToProposal(proposal);
		if (p != null) {
			nameCache.add(p.getDisplayString());
		}
	}

	private CompletionProposal castToProposal(Object obj) {
		if (obj instanceof CompletionProposal)
			return (CompletionProposal) obj;

		return null;
	}
}

Back to the top