/// <summary> /// Return a sorted list of variable names for variable dropdown menus. /// Include a special option at the end for creating a new variable name. /// </summary> /// <returns>Array of variable names.</returns> public JsArray <DropdownItemInfo> dropdownCreate() { JsArray <string> variableList; if (this.sourceBlock_ != null && this.sourceBlock_.workspace != null) { // Get a copy of the list, so that adding rename and new variable options // doesn't modify the workspace's list. variableList = this.sourceBlock_.workspace.variableList.Slice(0); } else { variableList = new JsArray <string>(); } // Ensure that the currently selected variable is an option. var name = this.getText(); if (name != null && variableList.IndexOf(name) == -1) { variableList.Push(name); } Array.Sort(variableList, CaseInsensitiveCompare.caseInsensitiveCompare); variableList.Push(Msg.RENAME_VARIABLE); variableList.Push(Msg.DELETE_VARIABLE.Replace("%1", name)); // Variables are not language-specific, use the name as both the user-facing // text and the internal representation. var options = new JsArray <DropdownItemInfo>(variableList.Length); for (var i = 0; i < variableList.Length; i++) { options[i] = new DropdownItemInfo(variableList[i], variableList[i]); } return(options); }
/// <summary> /// Factor out common words in statically defined options. /// Create prefix and/or suffix labels. /// </summary> private void trimOptions_() { this.prefixField = null; this.suffixField = null; var options = this.menuGenerator_.As <JsArray <DropdownItemInfo> >(); if (options == null || options.Length < 2) { return; } var strings = options.Map((t) => { return(t.text); }); var shortest = Core.shortestStringLength(strings); var prefixLength = Core.commonWordPrefix(strings, shortest); var suffixLength = Core.commonWordSuffix(strings, shortest); if (prefixLength == 0 && suffixLength == 0) { return; } if (shortest <= prefixLength + suffixLength) { // One or more strings will entirely vanish if we proceed. Abort. return; } if (prefixLength != 0) { this.prefixField = strings[0].Substring(0, prefixLength - 1); } if (suffixLength != 0) { this.suffixField = strings[0].Substr(1 - suffixLength); } // Remove the prefix and suffix from the options. var newOptions = new JsArray <DropdownItemInfo>(options.Length); for (var i = 0; i < options.Length; i++) { var text = options[i].text; var value = options[i].value; text = text.Substring(prefixLength, text.Length - suffixLength); newOptions[i] = new DropdownItemInfo(text, value); } this.menuGenerator_ = newOptions; }