internal void SetVisibleTypes(string text) { string part = ""; HMSItem item = GetHMSItemByText(text, out part); if (item != null) { if (item.Type.ToLower().StartsWith("bool")) { visibleItems.AddRange(HMS.ItemsBoolean); return; } HMSClassInfo info = HMS.HmsTypes[item.Type]; if (!string.IsNullOrEmpty(info.Name)) { visibleItems.AddRange(info.MemberItems); } } }
// > By WendyH ------------------------------------------ internal void DoAutocomplete(bool forced) { if (tb.IsDisposed) { return; } if (!Menu.Enabled || !this.Enabled) { Menu.Close(); return; } if (tb.CheckInTheStringOrComment()) { return; } if (!forced && Menu.AfterComplete) { Menu.AfterComplete = false; return; } visibleItems.Clear(); FocussedItemIndex = 0; VerticalScroll.Value = 0; //some magic for update scrolls AutoScrollMinSize -= new Size(1, 0); AutoScrollMinSize += new Size(1, 0); //get fragment around caret //Range fragment = tb.Selection.GetFragment(Menu.SearchPattern); Range fragment = tb.Selection.GetFragmentLookedLeft(); string text = fragment.Text; // < By WendyH ------------------------ bool doNotGetFromSourceItems = false; bool showTypes = false; if (text.Length == 0) { if (tb.ToolTip4Function.Visible && (HMS.CurrentParamType.Length > 0)) { if (HMS.CurrentParamType == "boolean") { visibleItems.AddRange(HMS.ItemsBoolean); doNotGetFromSourceItems = true; Menu.Fragment = fragment; } else if (HMS.HmsTypes.ContainsName(HMS.CurrentParamType)) { HMSClassInfo info = HMS.HmsTypes[HMS.CurrentParamType]; visibleItems.AddRange(info.MemberItems); doNotGetFromSourceItems = true; Menu.Fragment = fragment; } } else { text = tb.Selection.GetVariableForEqual(Menu.SearchPattern); if (text.Length > 0) { doNotGetFromSourceItems = true; forced = true; showTypes = true; } } } // > By WendyH ------------------------ //calc screen point for popup menu Point point = tb.PlaceToPoint(fragment.End); point.Offset(2, tb.CharHeight); // By WendyH if (tb.ToolTip4Function.Visible) { Rectangle b = tb.ToolTip4Function.Bounds; point.Y += b.Height + 4; } // if (forced || (text.Length >= Menu.MinFragmentLength && tb.Selection.IsEmpty && /*pops up only if selected range is empty*/ (tb.Selection.Start > fragment.Start || text.Length == 0 /*pops up only if caret is after first letter*/))) { Menu.Fragment = fragment; bool foundSelected = false; //build popup menu // < By WendyH ------------------------------------- string lastword = GetLastSelectedWord(text); if (showTypes) { SetVisibleTypes(text); } else { int indexDot = text.IndexOf("."); if (indexDot > 0) { SetVisibleMethods(text); doNotGetFromSourceItems = true; Menu.Fragment.ShiftStart(text.LastIndexOf('.') + 1); } else { foundSelected = GetActiveLastwordInVisibleItems(text, lastword); } } // > By WendyH ------------------------------------- if (!doNotGetFromSourceItems) { AutocompleteItems notExacctly = new AutocompleteItems(); foreach (var item in sourceItems) { item.Parent = Menu; CompareResult res = item.Compare(text); if (res != CompareResult.Hidden) { visibleItems.Add(item); } else if (item.NotExactlyCompare(text) == CompareResult.Visible) { notExacctly.Add(item); } if (lastword.Length > 0) { if (item.MenuText == lastword) { foundSelected = true; FocussedItemIndex = visibleItems.Count - 1; } } else if (res == CompareResult.VisibleAndSelected && !foundSelected) { foundSelected = true; FocussedItemIndex = visibleItems.Count - 1; } if (visibleItems.Count > 150) { break; } } visibleItems.AddRange(notExacctly); } if (foundSelected) { AdjustScroll(); //DoSelectedVisible(); } } if ((visibleItems.Count == 1) && (visibleItems[0].MenuText != null) && (visibleItems[0].MenuText.ToLower() == text.ToLower())) { return; } //show popup menu if (Count > 0) { if (!Menu.Visible) { CancelEventArgs args = new CancelEventArgs(); //// By WendyH //Point ps = tb.PointToScreen(point); //if (ps.Y + Menu.Height + (tb.CharHeight * 3) > SystemInformation.VirtualScreen.Bottom) { // int size = Math.Min(MaximumSize.Height, ItemHeight * visibleItems.Count) + 5; // point.Y -= (size + tb.CharHeight); // Rectangle b = tb.ToolTip4Function.Bounds; // if (tb.ToolTip4Function.Active) point.Y -= (b.Height + 4); //} Menu.OnOpening(args); if (!args.Cancel) { Menu.Show(tb, point); } } else { Invalidate(); DoSelectedVisible(); } } else { Menu.Close(); } }
// < By WendyH ------------------------------------------ public HMSItem GetHMSItemByText(string text, out string partAfterDot, bool returnItemBeforeDot = false) { HMSItem item = null; HMSClassInfo info = new HMSClassInfo(); string[] names = text.ToLower().Split('.'); int count = 0; partAfterDot = ""; foreach (string word in names) { string name = HMS.GetTextWithoutBrackets(word); count++; partAfterDot = name; if (returnItemBeforeDot && (count >= names.Length)) { break; // return last item before the dot } if (info.Name.Length > 0) { // search in class members item = info.MemberItems.GetItemOrNull(name); if (item == null) { item = info.StaticItems.GetItemOrNull(name); } if (item != null) { info = HMS.HmsClasses[item.Type]; } } else { // try get variabe if (item == null) { item = Menu.Items.VisibleLocalVars.GetItemOrNull(name); // try visible known variables } if (item == null) { item = Menu.Items.VisibleVariables.GetItemOrNull(name); // try visible known variables } if (item == null) { item = Menu.Items.VisibleFunctions.GetItemOrNull(name); // try functions in script } if (item == null) { item = HMS.ItemsVariable.GetItemOrNull(name); // try internal variables } if (item == null) { item = HMS.ItemsConstant.GetItemOrNull(name); // try internal constants } if (item == null) { item = HMS.ItemsFunction.GetItemOrNull(name); // try internal functions } if (item == null) { item = HMS.ItemsClass.GetItemOrNull(name); // try internal classes } if (count < names.Length) { if (item != null) { info = HMS.HmsClasses[item.Type]; } else { break; } } } } return(item); }
// < By WendyH ------------------------------------------ public HMSItem GetHMSItemByText(string text, out string partAfterDot, bool returnItemBeforeDot = false) { HMSItem item = null; HMSClassInfo info = new HMSClassInfo(); string[] names = text.ToLower().Split('.'); int count = 0; partAfterDot = ""; foreach (string word in names) { string name = HMS.GetTextWithoutBrackets(word); count++; partAfterDot = name; if (returnItemBeforeDot && (count >= names.Length)) break; // return last item before the dot if (info.Name.Length > 0) { // search in class members item = info.MemberItems.GetItemOrNull(name); if (item == null) item = info.StaticItems.GetItemOrNull(name); if (item != null) info = HMS.HmsClasses[item.Type]; } else { // try get variabe if (item == null) item = Menu.Items.VisibleLocalVars.GetItemOrNull(name); // try visible known variables if (item == null) item = Menu.Items.VisibleVariables.GetItemOrNull(name); // try visible known variables if (item == null) item = Menu.Items.VisibleFunctions.GetItemOrNull(name); // try functions in script if (item == null) item = HMS.ItemsVariable.GetItemOrNull(name); // try internal variables if (item == null) item = HMS.ItemsConstant.GetItemOrNull(name); // try internal constants if (item == null) item = HMS.ItemsFunction.GetItemOrNull(name); // try internal functions if (item == null) item = HMS.ItemsClass.GetItemOrNull(name); // try internal classes if (count < names.Length) { if (item != null) info = HMS.HmsClasses[item.Type]; else break; } } } return item; }
public static void InitAndLoadHMSKnowledgeDatabase() { CreateIfNotExistDirectory(WorkingDir, true); CreateIfNotExistDirectory(TemplatesDir); CreateIfNotExistDirectory(ThemesDir); InitItemsBoolean(); // Загружаем базу данных знаний о HMS (классы, типы, функции и т.п.) из ресурсов HmsTypesString = Regex.Replace(HmsTypesStringWithHelp, "{.*?}", "").ToLower(); Assembly assembly = Assembly.GetExecutingAssembly(); bool isStatic = false; lock (HmsClasses) { Stream stream = null; try { // Load classes items HMSItem item; stream = assembly.GetManifestResourceStream(ResourcePath + "hms_classes.txt"); if (stream != null) { using (var reader = new StreamReader(stream)) { stream = null; ClassesString = "|"; HMSClassInfo hmsclass = null; var line = reader.ReadLine(); while (line != null) { if ((line.Trim().Length < 1) || (line.StartsWith("*"))) { line = reader.ReadLine(); continue; } int indent = line.Length - line.TrimStart().Length; item = GetHmsItemFromLine(line); if (indent == 0) { // it's Class if (!HmsClasses.ContainsName(item.Text)) { hmsclass = new HMSClassInfo { Name = item.Text, Type = item.Type, Help = item.Help }; HmsClasses.Add(hmsclass); item.Kind = DefKind.Class; item.ImageIndex = ImagesIndex.Class; item.ToolTipTitle = "Класс " + item.Text; item.IsClass = true; item.ClassInfo = hmsclass; ItemsClass.Add(item); ClassesString += hmsclass.Name.ToLower() + "|"; } } else if (indent == 2) { // it's method or property of the class var cmd = item.ToolTipTitle; if (cmd.StartsWith("function" )) { item.ImageIndex = ImagesIndex.Method; item.Kind = DefKind.Method ; } else if (cmd.StartsWith("procedure")) { item.ImageIndex = ImagesIndex.Method; item.Kind = DefKind.Procedure; } else if (cmd.StartsWith("property" )) { item.ImageIndex = ImagesIndex.Field ; item.Kind = DefKind.Property ; } else if (cmd.StartsWith("index" )) { item.ImageIndex = ImagesIndex.Enum ; item.Kind = DefKind.Property ; } else if (cmd.StartsWith("event" )) { item.ImageIndex = ImagesIndex.Event ; item.Kind = DefKind.Event ; } var name = Regex.Replace(cmd, @"^(function|procedure|property|index property|event)\s+", ""); name = Regex.Match(name, @"\w+").Value.Trim(); if (name.Length < 1) name += " "; item.Text = name; item.MenuText = name; item.Level = 1; if (item.ImageIndex == ImagesIndex.Enum) item.Text = name + "[^]"; else if (item.ImageIndex == ImagesIndex.Method) { if (cmd.IndexOf('(')>0) item.Text = name + "(^)"; //else item.Text = name + "()"; } if (name.ToLower() == "create") { // hmm... only one static method isStatic = true; hmsclass?.StaticItems.Add(item); } else { isStatic = false; hmsclass?.MemberItems.Add(item); } } else if ((indent == 4) || (line[0] == '\t')) { // it's help for parameters of last method if (isStatic) { if (hmsclass?.StaticItems.Count > 0) { item = hmsclass.StaticItems[hmsclass.StaticItems.Count - 1]; item.Params.Add(StylishHelp(line)); } } else { if (hmsclass?.MemberItems.Count > 0) { item = hmsclass.MemberItems[hmsclass.MemberItems.Count - 1]; item.Params.Add(StylishHelp(line)); } } } line = reader.ReadLine(); } } } // For each Class look the derived class and add his methods (info1) and methods of derived class of derived class (info2) foreach (var classItem in HmsClasses) { if (classItem.Type.Length == 0) continue; // if no type - skip HMSClassInfo info1 = HmsClasses[classItem.Type]; // get derived class if (info1.Name.Length == 0) continue; // if no found - skip HMSClassInfo info2 = HmsClasses[info1.Type]; // get derived class of the derived class if (info2.Name.Length > 0) { foreach (var i2 in info2.MemberItems) if (!classItem.MemberItems.ContainsName(i2.MenuText)) classItem.MemberItems.Add(i2); foreach (var i2 in info2.StaticItems) if (!classItem.StaticItems.ContainsName(i2.MenuText)) classItem.StaticItems.Add(i2); } foreach (var i1 in info1.MemberItems) if (!classItem.MemberItems.ContainsName(i1.MenuText)) classItem.MemberItems.Add(i1); foreach (var i1 in info1.StaticItems) if (!classItem.StaticItems.ContainsName(i1.MenuText)) classItem.StaticItems.Add(i1); classItem.MemberItems.SortByMenuText(); classItem.StaticItems.SortByMenuText(); } // Load a built-in Types (Enumerates) stream = assembly.GetManifestResourceStream(ResourcePath + "hms_types.txt"); if (stream != null) { using (var reader = new StreamReader(stream)) { stream = null; string line; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("*") || (line.Trim().Length == 0)) continue; // Skip comments and blank lines item = GetHmsItemFromLine(line); if (!HmsTypes.ContainsName(item.Text)) { var hmsType = new HMSClassInfo { Name = item.Text, Type = item.Text, Help = item.Help }; string names = Regex.Match(line, @"\((.*?)\)").Groups[1].Value; foreach(string name in names.Split(',')) { item = new HMSItem { ImageIndex = ImagesIndex.Enum, Text = name, MenuText = name, ToolTipTitle = name, ToolTipText = "Перечисление типа " + hmsType.Name }; hmsType.MemberItems.Add(item); } HmsTypes.Add(hmsType); ClassesString += hmsType.Name.ToLower() + "|"; } } } } } catch (Exception e) { LogError(e.ToString()); } finally { // ReSharper disable once UseNullPropagation if (stream != null) stream.Dispose(); } } // Load a built-in Functions and Procedures items BuildAutocompleteItemsFromResourse(ResourcePath + "hms_func.txt", ImagesIndex.Procedure, "", ItemsFunction, DefKind.Function); // Load a built-in Variables BuildAutocompleteItemsFromResourse(ResourcePath + "hms_vars.txt" , ImagesIndex.Field, "Встроенная переменная", ItemsVariable, DefKind.Variable); // Load a built-in Constants BuildAutocompleteItemsFromResourse(ResourcePath + "hms_constants.txt", ImagesIndex.Enum , "Встроенная константа" , ItemsConstant, DefKind.Constant); foreach(var info in HmsTypes) { foreach(var typeitem in info.MemberItems) { ItemsConstant.Add(typeitem); } } // Check the self NotFoundedType = "|"; HmsTypesString += "int|long|void|bool|float|"; foreach (var q in HmsClasses) { CheckKnownType(q.Type); foreach (var a in q.MemberItems) CheckKnownType(a.Type); } foreach (var q in ItemsFunction) CheckKnownType(q.Type); foreach (var q in ItemsVariable) CheckKnownType(q.Type); foreach (var q in ItemsConstant) CheckKnownType(q.Type); string funcList = ""; foreach (var q in ItemsFunction) funcList += "|"+q.MenuText; funcList = funcList.Substring(1).Replace("|Int|", "|Int\\(|"); RegexHmsFunctions = new Regex(@"\b(" + funcList + @")\b", RegexOptions.IgnoreCase); string varsList = ""; foreach (var q in ItemsVariable) varsList += "|" + q.MenuText; varsList = varsList.Substring(1); RegexHmsVariables = new Regex(@"\b(" + varsList + @")\b", RegexOptions.IgnoreCase); varsList = "|true|false|nil|null"; foreach (var q in ItemsConstant) varsList += "|" + q.MenuText; varsList = varsList.Substring(1); RegexHmsConstants = new Regex(@"\b(" + varsList + @")\b", RegexOptions.IgnoreCase); ClassesString += NotFoundedType.ToLower(); HmsTypesString += ""; }
private void SearchClassesItemsInScriptDescrition(ref string xml) { lock (HMS.HmsClasses) { string xmlBlock = Regex.Match(xml, "<Classes>(.*?)</Classes>").Value; MatchCollection mc = Regex.Matches(xmlBlock, @"<item(.*?)</item>"); foreach (Match matchItem in mc) { var text = regexCutText.Match(matchItem.Value).Groups[1].Value; var descr = regexCutDesc.Match(matchItem.Value).Groups[1].Value; if (text.Length == 0) continue; var item = HMS.GetHmsItemFromLine(text); item.ImageIndex = ImagesIndex.Class; item.ToolTipText = "Класс"; item.Kind = DefKind.Class; item.Help = descr; var foundItem = HMS.ItemsClass.GetItemOrNull(item.MenuText); if (foundItem != null) { if (foundItem.Help.Length == 0) foundItem.Help = descr; } else { var hmsclass = new HMSClassInfo { Name = item.Text, Type = item.Type, Help = item.Help }; HMS.HmsClasses.Add(hmsclass); item.Kind = DefKind.Class; item.ImageIndex = ImagesIndex.Class; item.ToolTipTitle = "Класс " + item.Text; item.IsClass = true; item.ClassInfo = hmsclass; HMS.ItemsClass.Add(item); HMS.ClassesString += hmsclass.Name.ToLower() + "|"; //ScriptAutocompleteItems.Add(item); MatchCollection mcChild = Regex.Matches(matchItem.Groups[1].Value, "<item text=\"(.*?)\""); foreach (Match m in mcChild) { var childItem = HMS.GetHmsItemFromLine(m.Groups[1].Value); var cmd = childItem.ToolTipTitle; if (cmd.StartsWith("function" )) { childItem.ImageIndex = ImagesIndex.Method; childItem.Kind = DefKind.Method ; } else if (cmd.StartsWith("procedure")) { childItem.ImageIndex = ImagesIndex.Method; childItem.Kind = DefKind.Procedure; } else if (cmd.StartsWith("property" )) { childItem.ImageIndex = ImagesIndex.Field ; childItem.Kind = DefKind.Property ; } else if (cmd.StartsWith("index" )) { childItem.ImageIndex = ImagesIndex.Enum ; childItem.Kind = DefKind.Property ; } else if (cmd.StartsWith("event" )) { childItem.ImageIndex = ImagesIndex.Event ; childItem.Kind = DefKind.Event ; } var name = Regex.Replace(cmd, @"^(function|procedure|property|index property|event)\s+", ""); name = Regex.Match(name, @"\w+").Value.Trim(); if (name.Length < 1) name += " "; childItem.Text = name; childItem.MenuText = name; childItem.Level = 1; if (childItem.ImageIndex == ImagesIndex.Enum ) childItem.Text = name + "[^]"; else if (childItem.ImageIndex == ImagesIndex.Method) { if (cmd.IndexOf('(')>0) childItem.Text = name + "(^)"; } if (name.ToLower() == "create") hmsclass?.StaticItems.Add(childItem); else hmsclass?.MemberItems.Add(childItem); } //Console.WriteLine(kind.ToString() + " MenuText: " + item.MenuText); } } } }