129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13
|
131
|
14
|
|
15 import dwtx.jface.text.templates.SimpleTemplateVariableResolver; // packageimport
|
|
16 import dwtx.jface.text.templates.TemplateBuffer; // packageimport
|
|
17 import dwtx.jface.text.templates.TemplateContext; // packageimport
|
|
18 import dwtx.jface.text.templates.TemplateContextType; // packageimport
|
|
19 import dwtx.jface.text.templates.Template; // packageimport
|
|
20 import dwtx.jface.text.templates.TemplateVariable; // packageimport
|
|
21 import dwtx.jface.text.templates.TemplateException; // packageimport
|
|
22 import dwtx.jface.text.templates.TemplateTranslator; // packageimport
|
|
23 import dwtx.jface.text.templates.DocumentTemplateContext; // packageimport
|
|
24 import dwtx.jface.text.templates.GlobalTemplateVariables; // packageimport
|
|
25 import dwtx.jface.text.templates.InclusivePositionUpdater; // packageimport
|
|
26 import dwtx.jface.text.templates.TemplateProposal; // packageimport
|
|
27 import dwtx.jface.text.templates.ContextTypeRegistry; // packageimport
|
|
28 import dwtx.jface.text.templates.JFaceTextTemplateMessages; // packageimport
|
|
29 import dwtx.jface.text.templates.TemplateCompletionProcessor; // packageimport
|
|
30 import dwtx.jface.text.templates.TextTemplateMessages; // packageimport
|
|
31 import dwtx.jface.text.templates.TemplateVariableType; // packageimport
|
|
32 import dwtx.jface.text.templates.TemplateVariableResolver; // packageimport
|
|
33
|
129
|
34 module dwtx.jface.text.templates.PositionBasedCompletionProposal;
|
|
35
|
|
36 import dwt.dwthelper.utils;
|
|
37
|
|
38
|
|
39
|
|
40
|
|
41
|
|
42 import dwt.graphics.Image;
|
|
43 import dwt.graphics.Point;
|
|
44 import dwtx.core.runtime.Assert;
|
|
45 import dwtx.jface.text.BadLocationException;
|
|
46 import dwtx.jface.text.DocumentEvent;
|
|
47 import dwtx.jface.text.IDocument;
|
|
48 import dwtx.jface.text.ITextViewer;
|
|
49 import dwtx.jface.text.Position;
|
|
50 import dwtx.jface.text.contentassist.ICompletionProposal;
|
|
51 import dwtx.jface.text.contentassist.ICompletionProposalExtension2;
|
|
52 import dwtx.jface.text.contentassist.IContextInformation;
|
|
53
|
|
54
|
|
55 /**
|
|
56 * A position based completion proposal.
|
|
57 *
|
|
58 * @since 3.0
|
|
59 */
|
|
60 final class PositionBasedCompletionProposal : ICompletionProposal, ICompletionProposalExtension2 {
|
|
61
|
|
62 /** The string to be displayed in the completion proposal popup */
|
|
63 private String fDisplayString;
|
|
64 /** The replacement string */
|
|
65 private String fReplacementString;
|
|
66 /** The replacement position. */
|
|
67 private Position fReplacementPosition;
|
|
68 /** The cursor position after this proposal has been applied */
|
|
69 private int fCursorPosition;
|
|
70 /** The image to be displayed in the completion proposal popup */
|
|
71 private Image fImage;
|
|
72 /** The context information of this proposal */
|
|
73 private IContextInformation fContextInformation;
|
|
74 /** The additional info of this proposal */
|
|
75 private String fAdditionalProposalInfo;
|
|
76
|
|
77 /**
|
|
78 * Creates a new completion proposal based on the provided information. The replacement string is
|
|
79 * considered being the display string too. All remaining fields are set to <code>null</code>.
|
|
80 *
|
|
81 * @param replacementString the actual string to be inserted into the document
|
|
82 * @param replacementPosition the position of the text to be replaced
|
|
83 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
|
|
84 */
|
|
85 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition) {
|
|
86 this(replacementString, replacementPosition, cursorPosition, null, null, null, null);
|
|
87 }
|
|
88
|
|
89 /**
|
|
90 * Creates a new completion proposal. All fields are initialized based on the provided information.
|
|
91 *
|
|
92 * @param replacementString the actual string to be inserted into the document
|
|
93 * @param replacementPosition the position of the text to be replaced
|
|
94 * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
|
|
95 * @param image the image to display for this proposal
|
|
96 * @param displayString the string to be displayed for the proposal
|
|
97 * @param contextInformation the context information associated with this proposal
|
|
98 * @param additionalProposalInfo the additional information associated with this proposal
|
|
99 */
|
|
100 public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo) {
|
|
101 Assert.isNotNull(replacementString);
|
|
102 Assert.isTrue(replacementPosition !is null);
|
|
103
|
|
104 fReplacementString= replacementString;
|
|
105 fReplacementPosition= replacementPosition;
|
|
106 fCursorPosition= cursorPosition;
|
|
107 fImage= image;
|
|
108 fDisplayString= displayString;
|
|
109 fContextInformation= contextInformation;
|
|
110 fAdditionalProposalInfo= additionalProposalInfo;
|
|
111 }
|
|
112
|
|
113 /*
|
|
114 * @see ICompletionProposal#apply(IDocument)
|
|
115 */
|
|
116 public void apply(IDocument document) {
|
|
117 try {
|
|
118 document.replace(fReplacementPosition.getOffset(), fReplacementPosition.getLength(), fReplacementString);
|
|
119 } catch (BadLocationException x) {
|
|
120 // ignore
|
|
121 }
|
|
122 }
|
|
123
|
|
124 /*
|
|
125 * @see ICompletionProposal#getSelection(IDocument)
|
|
126 */
|
|
127 public Point getSelection(IDocument document) {
|
|
128 return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
|
|
129 }
|
|
130
|
|
131 /*
|
|
132 * @see ICompletionProposal#getContextInformation()
|
|
133 */
|
|
134 public IContextInformation getContextInformation() {
|
|
135 return fContextInformation;
|
|
136 }
|
|
137
|
|
138 /*
|
|
139 * @see ICompletionProposal#getImage()
|
|
140 */
|
|
141 public Image getImage() {
|
|
142 return fImage;
|
|
143 }
|
|
144
|
|
145 /*
|
|
146 * @see dwtx.jface.text.contentassist.ICompletionProposal#getDisplayString()
|
|
147 */
|
|
148 public String getDisplayString() {
|
|
149 if (fDisplayString !is null)
|
|
150 return fDisplayString;
|
|
151 return fReplacementString;
|
|
152 }
|
|
153
|
|
154 /*
|
|
155 * @see ICompletionProposal#getAdditionalProposalInfo()
|
|
156 */
|
|
157 public String getAdditionalProposalInfo() {
|
|
158 return fAdditionalProposalInfo;
|
|
159 }
|
|
160
|
|
161 /*
|
|
162 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#apply(dwtx.jface.text.ITextViewer, char, int, int)
|
|
163 */
|
|
164 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
|
|
165 apply(viewer.getDocument());
|
|
166 }
|
|
167
|
|
168 /*
|
|
169 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#selected(dwtx.jface.text.ITextViewer, bool)
|
|
170 */
|
|
171 public void selected(ITextViewer viewer, bool smartToggle) {
|
|
172 }
|
|
173
|
|
174 /*
|
|
175 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#unselected(dwtx.jface.text.ITextViewer)
|
|
176 */
|
|
177 public void unselected(ITextViewer viewer) {
|
|
178 }
|
|
179
|
|
180 /*
|
|
181 * @see dwtx.jface.text.contentassist.ICompletionProposalExtension2#validate(dwtx.jface.text.IDocument, int, dwtx.jface.text.DocumentEvent)
|
|
182 */
|
|
183 public bool validate(IDocument document, int offset, DocumentEvent event) {
|
|
184 try {
|
|
185 String content= document.get(fReplacementPosition.getOffset(), offset - fReplacementPosition.getOffset());
|
|
186 if (fReplacementString.startsWith(content))
|
|
187 return true;
|
|
188 } catch (BadLocationException e) {
|
|
189 // ignore concurrently modified document
|
|
190 }
|
|
191 return false;
|
|
192 }
|
|
193
|
|
194 }
|