Beispiel #1
0
        static string get_text_range(this IScintillaGateway document, int startPos, int endPos, int bufCapacity)
        {
            bool newNppVersion = IsNewNppApiVersion;

            if (Environment.GetEnvironmentVariable("CSSCRIPT_NPP_NEW_NPP_API") != null)
            {
                newNppVersion = Environment.GetEnvironmentVariable("CSSCRIPT_NPP_NEW_NPP_API").ToLower() == "true";
            }

            if (newNppVersion)
            {
                using (var tr = new TextRange((IntPtr)startPos, (IntPtr)endPos, bufCapacity))
                {
                    document.GetTextRange(tr);
                    return(tr.lpstrText);
                }
            }
            else
            {
                using (var tr = new TextRangeLegacy(startPos, endPos, bufCapacity))
                {
                    document.GetTextRangeLegacy(tr);
                    return(tr.lpstrText);
                }
            }
        }
        static public string TextBeforePosition(this IScintillaGateway document, int position, int maxLength)
        {
            int    bufCapacity      = maxLength + 1;
            IntPtr hCurrentEditView = PluginBase.GetCurrentScintilla();
            int    currentPos       = position;
            int    beginPos         = currentPos - maxLength;
            int    startPos         = (beginPos > 0) ? beginPos : 0;
            int    size             = currentPos - startPos;

            if (size > 0)
            {
                using (var tr = new TextRange(startPos, currentPos, bufCapacity))
                {
                    document.GetTextRange(tr);
                    return(tr.lpstrText);
                }
            }
            else
            {
                return(null);
            }
        }
Beispiel #3
0
        static internal void doInsertHtmlCloseTag(char newChar)
        {
            LangType docType = LangType.L_TEXT;

            Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETCURRENTLANGTYPE, 0, ref docType);
            bool isDocTypeHTML = (docType == LangType.L_HTML || docType == LangType.L_XML || docType == LangType.L_PHP);

            if (!doCloseTag || !isDocTypeHTML)
            {
                return;
            }

            if (newChar != '>')
            {
                return;
            }

            int bufCapacity = 512;
            var pos         = editor.GetCurrentPos();
            int currentPos  = pos;
            int beginPos    = currentPos - (bufCapacity - 1);
            int startPos    = (beginPos > 0) ? beginPos : 0;
            int size        = currentPos - startPos;

            if (size < 3)
            {
                return;
            }

            using (TextRange tr = new TextRange(startPos, currentPos, bufCapacity))
            {
                editor.GetTextRange(tr);
                string buf = tr.lpstrText;

                if (buf[size - 2] == '/')
                {
                    return;
                }

                int pCur = size - 2;
                while ((pCur > 0) && (buf[pCur] != '<') && (buf[pCur] != '>'))
                {
                    pCur--;
                }

                if (buf[pCur] == '<')
                {
                    pCur++;

                    var insertString = new StringBuilder("</");

                    while (regex.IsMatch(buf[pCur].ToString()))
                    {
                        insertString.Append(buf[pCur]);
                        pCur++;
                    }
                    insertString.Append('>');

                    if (insertString.Length > 3)
                    {
                        editor.BeginUndoAction();
                        editor.ReplaceSel(insertString.ToString());
                        editor.SetSel(pos, pos);
                        editor.EndUndoAction();
                    }
                }
            }
        }