From 1f90a60db1a7b5f7d98e5d11af9c7d13e0c25d9a Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Mon, 9 Mar 2020 18:11:02 +0200 Subject: [PATCH] Fixed StyledtextContentAdapter to overwrite existing content properly gitlab #492 Change-Id: I58e4feb40b13f8a9c6b0619776cf496d5f21a80b --- .../ui/assist/StyledTextContentAdapter.java | 62 +++++++++++++------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/assist/StyledTextContentAdapter.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/assist/StyledTextContentAdapter.java index dd71e323b..899b9e731 100644 --- a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/assist/StyledTextContentAdapter.java +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/assist/StyledTextContentAdapter.java @@ -10,6 +10,7 @@ *******************************************************************************/ 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; @@ -69,26 +70,51 @@ public class StyledTextContentAdapter implements IControlContentAdapter, IContro 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 match in + * text ending at offset endOffset. + * + * Example: + *
+	 * ...  res (=text)
+	 * resource (=match)
+	 *  resourc
+	 *   resour
+	 *    resou
+	 *     reso
+	 *      res match! return endOffset - 3
+	 * 
+ * + * @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; } /* -- 2.47.1