From e69d15ece7769af90a17fb6de5666d9073dc4066 Mon Sep 17 00:00:00 2001 From: miettinen Date: Tue, 11 Sep 2012 10:11:18 +0000 Subject: [PATCH] Improving the content assist in text fields (refs #2969): * ESC no longer destroys all changes if pressed when the popup box is open * The equation doesn't need to be delimited by whitespace to make the auto completion work. * The cursor is put in between the parenthesis of functions * Variables are on top of the list Small fix to completion processor in the charts: '_' character is now allowed too git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25710 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../expressions/CompletionProcessor.java | 37 ++++++++++++------- .../widgets/expressions/ExpressionField.java | 9 +++-- .../trend/chart/properties/RVIModifier.java | 2 +- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java index d0966676..533fc146 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/CompletionProcessor.java @@ -49,9 +49,10 @@ public class CompletionProcessor implements IContentAssistProcessor { private char[] allowedCharacters = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Å','Ä','Ö', - '1','2','3','4','5','6','7','8','9','0','.', - '(',')'}; + '1','2','3','4','5','6','7','8','9','0','.','_','(',')'}; + private String allowedConnectedCharactersRegExp = "[\\Q({[:;,<=>+-*/^\\E]"; + public CompletionProcessor(Table allowedVariables, boolean allowFunctions){ this.allowedVariables = allowedVariables; @@ -87,7 +88,10 @@ public class CompletionProcessor implements IContentAssistProcessor { e.printStackTrace(); } } - Collections.sort(functions); + Collections.sort(functions); + for (int i = 0; i < functions.size(); ++i) { + functions.set(i, functions.get(i) + "()"); + } } private ICompletionProposal[] collectProposals(String token, int offset) { @@ -104,14 +108,6 @@ public class CompletionProcessor implements IContentAssistProcessor { } ArrayList resultArray = new ArrayList(); - for (String function : functions) { - if (token.length() == 0 || function.toUpperCase().startsWith(token.toUpperCase())) { - resultArray.add(new CompletionProposal(function, - offset - token.length(), - token.length(), - function.length())); - } - } for (String variable : variables) { if (token.length() == 0 || variable.toUpperCase().startsWith(token.toUpperCase())) { resultArray.add(new CompletionProposal(variable, @@ -120,6 +116,14 @@ public class CompletionProcessor implements IContentAssistProcessor { variable.length())); } } + for (String function : functions) { + if (token.length() == 0 || function.toUpperCase().startsWith(token.toUpperCase())) { + resultArray.add(new CompletionProposal(function, + offset - token.length(), + token.length(), + function.length() - 1)); + } + } ICompletionProposal[] result = new ICompletionProposal[resultArray.size()]; for (int i = 0; i < result.length; ++i) { result[i] = resultArray.get(i); @@ -131,7 +135,6 @@ public class CompletionProcessor implements IContentAssistProcessor { public ICompletionProposal[] computeCompletionProposals( ITextViewer viewer, int offset) { String equation = viewer.getDocument().get(); -// System.out.println(equation + "\noffset = " + offset); if (equation.length() == 0 || offset == 0 @@ -140,14 +143,20 @@ public class CompletionProcessor implements IContentAssistProcessor { } equation = equation.substring(0, offset); -// System.out.println(equation + "\noffset = " + offset); // Split into tokens on whitespace characters - String[] tokens = equation.split("\\s+"); + String[] tokens = equation.split("[\\s]"); if (tokens.length == 0) { return collectProposals("", offset); } String token = tokens[tokens.length - 1]; + + // Split the last token on '+', '-', etc. characters + String tokensOfLastToken[] = token.split(allowedConnectedCharactersRegExp); + if (tokensOfLastToken.length == 0) { + return collectProposals("", offset); + } + token = tokensOfLastToken[tokensOfLastToken.length - 1]; // System.out.println(token + "\noffset = " + offset); return collectProposals(token, offset); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java index 7b79a518..39e7381d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionField.java @@ -134,9 +134,12 @@ public class ExpressionField extends Composite { @Override public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC && getExpression() != null) { - ((StyledText)e.widget).setText(oldExpression); - ((StyledText)e.widget).setSelection(getExpression().length()); + // Check if the expression field has an active completion assistant + if (!isAssistSessionActive()) { + if(e.keyCode == SWT.ESC && getExpression() != null) { + ((StyledText)e.widget).setText(oldExpression); + ((StyledText)e.widget).setSelection(getExpression().length()); + } } } }); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/RVIModifier.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/RVIModifier.java index 9a667b8e..27246f5b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/RVIModifier.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/RVIModifier.java @@ -44,7 +44,7 @@ public class RVIModifier extends TextModifyListenerImpl { private char[] alphaNumericCharacters = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Å','Ä','Ö', - '1','2','3','4','5','6','7','8','9','0','.'}; + '1','2','3','4','5','6','7','8','9','0','.','_'}; /** * Create a new RVIModifier and attach a content proposal support to control -- 2.47.1