Esempio n. 1
0
        /// <summary>
        /// Replace the tag at the caret with an expansion defined in the [Tags]
        /// ini-file section.
        /// </summary>
        internal static void ReplaceTag()
        {
            IntPtr           currentScint     = PluginBase.GetCurrentScintilla();
            ScintillaGateway scintillaGateway = new ScintillaGateway(currentScint);
            int position = scintillaGateway.GetSelectionEnd();

            string selectedText = scintillaGateway.GetSelText();

            if (string.IsNullOrEmpty(selectedText))
            {
                // TODO: remove this hardcoded 10 crap. Remove selection manipulation:
                // user will not be happy to see any such side-effects.
                scintillaGateway.SetSelection(position > 10 ? (position - 10) : (position - position), position);
                selectedText = scintillaGateway.GetSelText();
                var reges = Regex.Matches(scintillaGateway.GetSelText(), @"(\w+)");
                if (reges.Count > 0)
                {
                    selectedText = reges.Cast <Match>().Select(m => m.Value).LastOrDefault();
                    scintillaGateway.SetSelection(position - selectedText.Length, position);
                    selectedText = scintillaGateway.GetSelText();
                }
            }
            try {
                if (string.IsNullOrEmpty(selectedText))
                {
                    throw new Exception("No tag here.");
                }
                byte[] buffer = new byte[1048];
                var    ini    = new IniFile(iniFilePath);
                string value  = ini.Get("Tags", selectedText, 1048);
                if (string.IsNullOrEmpty(value.Trim('\0')))
                {
                    throw new Exception("No tag here.");
                }
                value = TransformTags(value);
                scintillaGateway.ReplaceSel(value.Replace("|", null));
                scintillaGateway.SetSelectionEnd(position + value.Substring(0, value.IndexOf('|')).Length - selectedText.Length);
            } catch (Exception ex) {
                scintillaGateway.CallTipShow(position, ex.Message);
            }
        }