1 package org.simantics.document.ui;
3 import org.eclipse.core.runtime.IProgressMonitor;
4 import org.eclipse.jface.text.BadLocationException;
5 import org.eclipse.jface.text.DefaultInformationControl;
6 import org.eclipse.jface.text.DocumentEvent;
7 import org.eclipse.jface.text.IDocument;
8 import org.eclipse.jface.text.IInformationControl;
9 import org.eclipse.jface.text.IInformationControlCreator;
10 import org.eclipse.jface.text.ITextViewer;
11 import org.eclipse.jface.text.contentassist.ICompletionProposal;
12 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
13 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
14 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3;
15 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
16 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension5;
17 import org.eclipse.jface.text.contentassist.IContextInformation;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.widgets.Shell;
21 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
23 public class CSSCompletionProposal implements ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2, ICompletionProposalExtension3, ICompletionProposalExtension4, ICompletionProposalExtension5 {
25 private static final Image PRIVATE = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/private_co.gif").createImage();
26 private static final Image PUBLIC = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/public_co.gif").createImage();
27 private static final Image CONST = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/container_obj.gif").createImage();
28 private static final Image TYPE = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/typedef_obj.gif").createImage();
30 private final String content;
31 private final String name;
32 private final String module;
33 private final String documentation;
34 private int replacementOffset;
35 private String prefix;
36 private final CSSCompletionType completionType;
38 public CSSCompletionProposal(SCLValue value, int replacementOffset, String prefix) {
39 this.name = value.getName().name;
40 this.module = value.getName().module;
41 this.documentation = value.getDocumentation();
42 this.content = name + " :: " + value.getType() + " (" + module + ")";
43 this.replacementOffset = replacementOffset;
45 // System.out.println(prefix);
46 if (value.isPrivate())
47 this.completionType = CSSCompletionType.PRIVATE;
49 this.completionType = CSSCompletionType.PUBLIC;
52 public CSSCompletionProposal(String name, String module, CSSCompletionType completionType, int replacementOffset, String prefix) {
55 this.content = name + " (" + module + ")";
56 this.documentation = null;
57 this.replacementOffset = replacementOffset;
59 this.completionType = completionType;
63 public void apply(IDocument document) {
68 public Point getSelection(IDocument document) {
69 return new Point(replacementOffset + getName().length(), 0);
73 public String getAdditionalProposalInfo() {
78 public String getDisplayString() {
83 public Image getImage() {
84 switch (completionType) {
99 public IContextInformation getContextInformation() {
104 public void apply(IDocument document, char trigger, int offset) {
108 public boolean isValidFor(IDocument document, int offset) {
109 return validate(document, offset, null);
113 public char[] getTriggerCharacters() {
118 public int getContextInformationPosition() {
123 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
125 // String replacement = getName().substring(offset - replacementOffset);
126 IDocument doc = viewer.getDocument();
127 Point p = viewer.getSelectedRange();
128 // System.out.println("selectedRange : " + p);
129 // System.out.println("prefix : " + prefix);
130 // int start = offset - prefix.length();
131 // int end = prefix.length();
132 // System.out.println("start : " + start + ", end : " + end);
133 // String sadd = doc.get(start, end);
134 // System.out.println("toReplace : " + sadd);
136 doc.replace(p.x, p.y, "");
137 doc.replace(offset, 0, getName());
139 String currentText = doc.get(offset - prefix.length(), prefix.length());
140 if (currentText.equals(getName()))
142 doc.replace(offset - prefix.length(), prefix.length(), "");
143 doc.replace(offset - prefix.length(), 0, getName());
145 } catch (BadLocationException x) {
151 public void selected(ITextViewer viewer, boolean smartToggle) {
155 public void unselected(ITextViewer viewer) {
159 public boolean validate(IDocument document, int offset, DocumentEvent event) {
161 // System.out.println("replacementOffset : " + replacementOffset);
162 // System.out.println("offset : " + offset);
163 boolean a = offset >= replacementOffset;
164 boolean b = offset < replacementOffset + getName().length();
165 String s = document.get(replacementOffset, offset - replacementOffset);
167 String d = getName();//.substring(0, offset - prefixStart);
168 boolean c = d.toLowerCase().startsWith(s.toLowerCase());
170 } catch (BadLocationException x) {
171 //x.printStackTrace();
177 public IInformationControlCreator getInformationControlCreator() {
181 private static final IInformationControlCreator CREATOR = new IInformationControlCreator() {
184 public IInformationControl createInformationControl(Shell parent) {
185 return new DefaultInformationControl(parent);
190 public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) {
195 public int getPrefixCompletionStart(IDocument document, int completionOffset) {
196 return replacementOffset - prefix.length();
200 public boolean isAutoInsertable() {
205 public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
206 return documentation;
209 public String getName() {
213 public boolean isPrivate() {
214 return CSSCompletionType.PRIVATE == completionType;
217 public String getModule() {