]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/completion/SCLCompletionProposal.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor / completion / SCLCompletionProposal.java
1 package org.simantics.scl.ui.editor.completion;\r
2 \r
3 import org.eclipse.core.runtime.IProgressMonitor;\r
4 import org.eclipse.jface.text.BadLocationException;\r
5 import org.eclipse.jface.text.DefaultInformationControl;\r
6 import org.eclipse.jface.text.DocumentEvent;\r
7 import org.eclipse.jface.text.IDocument;\r
8 import org.eclipse.jface.text.IInformationControl;\r
9 import org.eclipse.jface.text.IInformationControlCreator;\r
10 import org.eclipse.jface.text.ITextViewer;\r
11 import org.eclipse.jface.text.contentassist.ICompletionProposal;\r
12 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;\r
13 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;\r
14 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3;\r
15 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;\r
16 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension5;\r
17 import org.eclipse.jface.text.contentassist.IContextInformation;\r
18 import org.eclipse.swt.graphics.Image;\r
19 import org.eclipse.swt.graphics.Point;\r
20 import org.eclipse.swt.widgets.Shell;\r
21 import org.simantics.scl.compiler.elaboration.modules.SCLValue;\r
22 import org.simantics.scl.ui.Activator;\r
23 \r
24 public class SCLCompletionProposal implements ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2, ICompletionProposalExtension3, ICompletionProposalExtension4, ICompletionProposalExtension5 {\r
25     \r
26     private static final Image PRIVATE = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/private_co.gif").createImage();\r
27     private static final Image PUBLIC = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/public_co.gif").createImage();\r
28     private static final Image CONST = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/container_obj.gif").createImage();\r
29     private static final Image TYPE = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/typedef_obj.gif").createImage();\r
30     \r
31     private final String content;\r
32     private final String name;\r
33     private final String module;\r
34     private final String documentation;\r
35     private int replacementOffset;\r
36     private String prefix;\r
37     private final SCLCompletionType completionType;\r
38     \r
39     public SCLCompletionProposal(SCLValue value, int replacementOffset, String prefix) {\r
40         this.name = value.getName().name;\r
41         this.module = value.getName().module;\r
42         this.documentation = value.getDocumentation();\r
43         this.content = name + " :: " + value.getType() + "  (" + module + ")";\r
44         this.replacementOffset = replacementOffset;\r
45         this.prefix = prefix;\r
46 //        System.out.println(prefix);\r
47         if (value.isPrivate())\r
48             this.completionType = SCLCompletionType.PRIVATE;\r
49         else\r
50             this.completionType = SCLCompletionType.PUBLIC;\r
51     }\r
52 \r
53     public SCLCompletionProposal(String name, String module, SCLCompletionType completionType, int replacementOffset, String prefix) {\r
54         this.name = name;\r
55         this.module = module;\r
56         this.content = name + " (" + module + ")";\r
57         this.documentation = null;\r
58         this.replacementOffset = replacementOffset;\r
59         this.prefix = prefix;\r
60         this.completionType = completionType;\r
61     }\r
62 \r
63     @Override\r
64     public void apply(IDocument document) {\r
65 \r
66     }\r
67 \r
68     @Override\r
69     public Point getSelection(IDocument document) {\r
70         return new Point(replacementOffset + getName().length(), 0);\r
71     }\r
72 \r
73     @Override\r
74     public String getAdditionalProposalInfo() {\r
75         return documentation;\r
76     }\r
77 \r
78     @Override\r
79     public String getDisplayString() {\r
80         return content;\r
81     }\r
82 \r
83     @Override\r
84     public Image getImage() {\r
85         switch (completionType) {\r
86         case PRIVATE:\r
87             return PRIVATE;\r
88         case PUBLIC:\r
89             return PUBLIC;\r
90         case CONST:\r
91             return CONST;\r
92         case TYPE:\r
93             return TYPE;\r
94         default:\r
95             return null;\r
96         }\r
97     }\r
98 \r
99     @Override\r
100     public IContextInformation getContextInformation() {\r
101         return null;\r
102     }\r
103 \r
104     @Override\r
105     public void apply(IDocument document, char trigger, int offset) {\r
106     }\r
107 \r
108     @Override\r
109     public boolean isValidFor(IDocument document, int offset) {\r
110         return validate(document, offset, null);\r
111     }\r
112 \r
113     @Override\r
114     public char[] getTriggerCharacters() {\r
115         return null;\r
116     }\r
117 \r
118     @Override\r
119     public int getContextInformationPosition() {\r
120         return 0;\r
121     }\r
122 \r
123     @Override\r
124     public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {\r
125         try {\r
126 //            String replacement = getName().substring(offset - replacementOffset);\r
127             IDocument doc = viewer.getDocument();\r
128             Point p = viewer.getSelectedRange();\r
129 //            System.out.println("selectedRange : " + p);\r
130 //            System.out.println("prefix : " + prefix);\r
131 //            int start = offset - prefix.length();\r
132 //            int end = prefix.length();\r
133 //            System.out.println("start : " + start + ", end : " + end);\r
134 //            String sadd = doc.get(start, end);\r
135 //            System.out.println("toReplace : " + sadd);\r
136             if (p.y > 0) {\r
137                 doc.replace(p.x, p.y, "");\r
138                 doc.replace(offset, 0, getName());\r
139             } else {\r
140                 String currentText = doc.get(offset - prefix.length(), prefix.length());\r
141                 if (currentText.equals(getName()))\r
142                     return;\r
143                 doc.replace(offset - prefix.length(), prefix.length(), "");\r
144                 doc.replace(offset - prefix.length(), 0, getName());\r
145             }\r
146         } catch (BadLocationException x) {\r
147             x.printStackTrace();\r
148         }\r
149     }\r
150 \r
151     @Override\r
152     public void selected(ITextViewer viewer, boolean smartToggle) {\r
153     }\r
154 \r
155     @Override\r
156     public void unselected(ITextViewer viewer) {\r
157     }\r
158 \r
159     @Override\r
160     public boolean validate(IDocument document, int offset, DocumentEvent event) {\r
161         try {\r
162 //            System.out.println("replacementOffset : " + replacementOffset);\r
163 //            System.out.println("offset : " + offset);\r
164             boolean a = offset >= replacementOffset;\r
165             boolean b = offset < replacementOffset + getName().length();\r
166             String s = document.get(replacementOffset, offset - replacementOffset);\r
167             prefix = s;\r
168             String d = getName();//.substring(0, offset - prefixStart);\r
169             boolean c = d.toLowerCase().startsWith(s.toLowerCase());\r
170             return a && b && c;\r
171         } catch (BadLocationException x) {\r
172             //x.printStackTrace();\r
173             return false;\r
174         }\r
175     }\r
176 \r
177     @Override\r
178     public IInformationControlCreator getInformationControlCreator() {\r
179         return CREATOR;\r
180     }\r
181     \r
182     private static final IInformationControlCreator CREATOR = new IInformationControlCreator() {\r
183         \r
184         @Override\r
185         public IInformationControl createInformationControl(Shell parent) {\r
186             return new DefaultInformationControl(parent);\r
187         }\r
188     };\r
189 \r
190     @Override\r
191     public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) {\r
192         return getName();\r
193     }\r
194 \r
195     @Override\r
196     public int getPrefixCompletionStart(IDocument document, int completionOffset) {\r
197         return replacementOffset - prefix.length();\r
198     }\r
199 \r
200     @Override\r
201     public boolean isAutoInsertable() {\r
202         return true;\r
203     }\r
204 \r
205     @Override\r
206     public Object getAdditionalProposalInfo(IProgressMonitor monitor) {\r
207         return documentation;\r
208     }\r
209 \r
210     public String getName() {\r
211         return name;\r
212     }\r
213     \r
214     public boolean isPrivate() {\r
215         return SCLCompletionType.PRIVATE == completionType;\r
216     }\r
217 \r
218     public String getModule() {\r
219         return module;\r
220     }\r
221 }\r