*******************************************************************************/
package org.simantics.scl.ui.assist;
+import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.IControlContentAdapter;
import org.eclipse.jface.fieldassist.IControlContentAdapter2;
import org.eclipse.swt.custom.StyledText;
public void insertControlContents(Control control, String contents, int cursorPosition) {
StyledText text = ((StyledText)control);
cursorPosition = Math.min(cursorPosition, contents.length());
- int caretEndRange = text.getCaretOffset();
+
+ int caretOffset = text.getCaretOffset();
String currentText = text.getText();
-
- int offset = caretEndRange;
- int length = currentText.length();
- while (--offset >= 0 && (Character.isJavaIdentifierPart(currentText.charAt(offset)) && !Character.isWhitespace(currentText.charAt(offset))))
- length--;
-
- int nameSpaceBeginRange = currentText.lastIndexOf(".", caretEndRange - 1); //$NON-NLS-1$
- if (nameSpaceBeginRange > length)
- length = nameSpaceBeginRange;
- int endRange = currentText.length();
- if (caretEndRange < endRange)
- endRange = caretEndRange;
- text.setSelection(length, endRange);
+ int replacementOffset = findPrefixMatchOffset(currentText, caretOffset, contents);
+
+// System.out.println("text: " + currentText);
+// System.out.println("proposal to fill: " + contents);
+// System.out.format("longest match of proposed contents found from text @ offset %d: \"%s[%s]%s\"%n",
+// replacementOffset,
+// currentText.substring(0, replacementOffset),
+// currentText.substring(replacementOffset, caretOffset),
+// currentText.substring(caretOffset));
+
+ // The text between [replaceOffset, caretOffset) will be replaced with `contents`
+ text.setSelection(replacementOffset, caretOffset);
text.insert(contents);
- // calculate the initial count of letters that was typed when the proposal was accepted to insert the caret
- // at the right position
- int proposalFirstLettersCount = endRange - (length);
- text.setCaretOffset(caretEndRange + cursorPosition - proposalFirstLettersCount);
+ text.setSelection(replacementOffset + contents.length());
+ }
+
+ /**
+ * Find offset of longest prefix match of <code>match</code> in
+ * <code>text</code> ending at offset <code>endOffset</code>.
+ *
+ * Example:
+ * <pre>
+ * ... res (=text)
+ * resource (=match)
+ * resourc
+ * resour
+ * resou
+ * reso
+ * res match! return endOffset - 3
+ * </pre>
+ *
+ * @param text the text from which to find the match
+ * @param endOffset endOffset until which to search for the longest match
+ * @param match the text to prefix-match
+ * @return
+ */
+ private static int findPrefixMatchOffset(String text, int endOffset, String match) {
+ for (int i = match.length(); i >= 0; --i) {
+ if (text.regionMatches(true, endOffset - i, match, 0, i))
+ return endOffset - i;
+ }
+ return 0;
}
/*