void _readNativeStruct()
 {
     if (_ptrSciTextRange != IntPtr.Zero)
     {
         _sciTextRange = (Sci_TextRange)Marshal.PtrToStructure(_ptrSciTextRange, typeof(Sci_TextRange));
     }
 }
Example #2
0
 private static string GetTextRange(int startPos, int endPos)
 {
     using (var sciTextRange = new Sci_TextRange(startPos, endPos, endPos - startPos + 1))
     {
         var hCurrentEditView = PluginBase.GetCurrentScintilla();
         Win32.SendMessage(hCurrentEditView, SciMsg.SCI_GETTEXTRANGE, 0, sciTextRange.NativePointer);
         return(sciTextRange.lpstrText);
     }
 }
Example #3
0
 /// <summary>
 /// Returns the text between the positions start and end. If end is -1, text is returned to the end of the document.
 /// The text is 0 terminated, so you must supply a buffer that is at least 1 character longer than the number of characters
 /// you wish to read. The return value is the length of the returned text not including the terminating 0.
 /// </summary>
 public string GetTextByRange(int start, int end, int bufCapacity)
 {
     using (var textRange = new Sci_TextRange(start, end, bufCapacity))
     {
         Call(SciMsg.SCI_GETTEXTRANGE, 0, textRange.NativePointer);
         //return textRange.lpstrText;
         return(IsUnicode() ? StringUtils.AnsiToUnicode(textRange.lpstrText) : textRange.lpstrText);
     }
 }
Example #4
0
        /// <summary>
        /// Gets all text of the current document.
        /// </summary>
        /// <returns></returns>
        static public string GetAllText()
        {
            int fullLength = (int)Win32.SendMessage(Npp.CurrentScintilla, SciMsg.SCI_GETLENGTH, 0, 0);

            using (var tr = new Sci_TextRange(0, fullLength, fullLength + 1))
            {
                Win32.SendMessage(Npp.CurrentScintilla, SciMsg.SCI_GETTEXTRANGE, 0, tr.NativePointer);
                return(tr.lpstrText);
            }
        }
Example #5
0
            public AutocompleteForm()
            {
                try
                {
                    // Get the position of the overall window
                    RECT mainWindowPosition;
                    GetWindowRect(PluginBase.GetCurrentScintilla(), out mainWindowPosition);
                    // Get the cursor postion, offset from the window position
                    int currentPos   = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETCURRENTPOS, 0, 0);
                    int caretOffsetX = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_POINTXFROMPOSITION, 0, currentPos);
                    int caretOffsetY = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_POINTYFROMPOSITION, 0, currentPos);
                    // Get the height of each line in pixels, so the autocomplete pop-up can be offset to fall underneath the current line
                    int lineHeight = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_TEXTHEIGHT, 0, 0);

                    // Determine coordinates for placing the autocomplete popup, by adding aLl the offsets to the NPP window coordinates
                    int positionX = mainWindowPosition.Left + caretOffsetX;
                    int positionY = mainWindowPosition.Top + caretOffsetY + lineHeight;

                    // Configure WinForms attributes
                    MaximizeBox     = false;
                    MinimizeBox     = false;
                    FormBorderStyle = FormBorderStyle.None;
                    Size            = new Size(400, 100);
                    StartPosition   = FormStartPosition.Manual;
                    Location        = new System.Drawing.Point(positionX, positionY);
                    AutoScroll      = true;
                    //KeyPreview = true;

                    // Add a ListBox for autocomplete suggestions
                    _suggestions      = new ListBox();
                    _suggestions.Dock = System.Windows.Forms.DockStyle.Fill;
                    _suggestions.SelectedIndexChanged += new EventHandler(_suggestions_SelectedIndexChanged);
                    _suggestions.DoubleClick          += new EventHandler(_suggestions_DoubleClick);
                    Controls.Add(_suggestions);

                    // Store the original word and its starting point, so that it can later be replaced or restored
                    int cursorPosition = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETCURRENTPOS, 0, 0);
                    _wordStartPosition = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_WORDSTARTPOSITION, cursorPosition, 1);
                    _originalWord      = "";
                    int    bufferCapacity = 1024;
                    string currentWord    = "";
                    using (Sci_TextRange textRange = new Sci_TextRange(_wordStartPosition, cursorPosition, bufferCapacity))
                    {
                        Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETTEXTRANGE, 0, textRange.NativePointer);
                        _originalWord = textRange.lpstrText;
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("An error occurred with the GoAutocomplete plugin:\n\n" + e.Message);
                }
            }
Example #6
0
        static public string GetTextBetween(int start, int end = -1)
        {
            IntPtr sci = Plugin.GetCurrentScintilla();

            if (end == -1)
            {
                end = (int)Win32.SendMessage(sci, SciMsg.SCI_GETLENGTH, 0, 0);
            }

            using (var tr = new Sci_TextRange(start, end, end - start + 1)) //+1 for null termination
            {
                Win32.SendMessage(sci, SciMsg.SCI_GETTEXTRANGE, 0, tr.NativePointer);
                return(tr.lpstrText);
            }
        }
Example #7
0
        /// <summary>
        /// Get the current cursor at the forward word
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentWord()
        {
            int currentPos = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETCURRENTPOS, 0, 0);
            int size       = 64;
            int beg        = currentPos - (size - 1);

            beg = beg > 0 ? beg : 0;
            int end = currentPos;

            size = end - beg;
            Sci_TextRange txtRange = new Sci_TextRange(beg, end, 64);

            Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETTEXTRANGE, 0, txtRange.NativePointer);
            string[] arr = txtRange.lpstrText.Split(_Spliter);
            return(arr[arr.Length - 1]);
        }
Example #8
0
        internal static String getSelectedText()
        {
            Util.BEGINFUN("getSelectedText");

            try
            {
                IntPtr editHandle = GetCurrentEditHandle();

                int cpMin = (int)Win32.SendMessage(editHandle, SciMsg.SCI_GETSELECTIONSTART, 0, 0);
                int cpMax = (int)Win32.SendMessage(editHandle, SciMsg.SCI_GETSELECTIONEND, 0, 0);

                Sci_TextRange tr = new Sci_TextRange(cpMin, cpMax, cpMax - cpMin + 1);

                Win32.SendMessage(editHandle, SciMsg.SCI_GETTEXTRANGE, 0, tr.NativePointer);

                string selected = tr.lpstrText;
                if (selected.Length < 1)
                {
                    return("");
                }

                Util.writeInfoLog("Selected text range: " + selected);

                logEncodingInfo();

                Encoding w1252     = Encoding.GetEncoding(1252);
                string   converted = Encoding.UTF8.GetString(w1252.GetBytes(selected));

                Util.writeInfoLog("Final selected text after conversion: " + converted);

                Util.ENDFUN("getSelectedText");

                return(converted);
            }
            catch (Exception ex)
            {
                handleException(ex);
                return("");
            }
        }
Example #9
0
        static public string TextBeforePosition(int position, int maxLength)
        {
            int    bufCapacity      = maxLength + 1;
            IntPtr hCurrentEditView = Plugin.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 Sci_TextRange(startPos, currentPos, bufCapacity))
                {
                    Win32.SendMessage(hCurrentEditView, SciMsg.SCI_GETTEXTRANGE, 0, tr.NativePointer);
                    return(tr.lpstrText);
                }
            }
            else
            {
                return(null);
            }
        }
Example #10
0
        static public string TextAfterPosition(int position, int maxLength)
        {
            int    bufCapacity      = maxLength + 1;
            IntPtr hCurrentEditView = Plugin.GetCurrentScintilla();
            int    currentPos       = position;
            int    fullLength       = (int)Win32.SendMessage(hCurrentEditView, SciMsg.SCI_GETLENGTH, 0, 0);
            int    startPos         = currentPos;
            int    endPos           = Math.Min(currentPos + bufCapacity, fullLength);
            int    size             = endPos - startPos;

            if (size > 0)
            {
                using (var tr = new Sci_TextRange(startPos, endPos, bufCapacity))
                {
                    Win32.SendMessage(hCurrentEditView, SciMsg.SCI_GETTEXTRANGE, 0, tr.NativePointer);
                    return(tr.lpstrText);
                }
            }
            else
            {
                return(null);
            }
        }
Example #11
0
        /// <summary>
        /// Returns text after the current caret position.
        /// </summary>
        /// <param name="maxLength">The maximum length.</param>
        /// <returns></returns>
        static public string TextAfterCaret(int maxLength = 512)
        {
            int    bufCapacity = maxLength + 1;
            IntPtr sci         = Npp.CurrentScintilla;
            int    currentPos  = (int)Win32.SendMessage(sci, SciMsg.SCI_GETCURRENTPOS, 0, 0);
            int    fullLength  = (int)Win32.SendMessage(sci, SciMsg.SCI_GETLENGTH, 0, 0);
            int    startPos    = currentPos;
            int    endPos      = Math.Min(currentPos + bufCapacity, fullLength);
            int    size        = endPos - startPos;

            if (size > 0)
            {
                using (var tr = new Sci_TextRange(startPos, endPos, bufCapacity))
                {
                    Win32.SendMessage(sci, SciMsg.SCI_GETTEXTRANGE, 0, tr.NativePointer);
                    return(tr.lpstrText);
                }
            }
            else
            {
                return(null);
            }
        }
Example #12
0
        static internal void doInsertHtmlCloseTag(char newChar)
        {
            LangType docType = LangType.L_TEXT;

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

            if (doCloseTag && isDocTypeHTML)
            {
                if (newChar == '>')
                {
                    int    bufCapacity      = 512;
                    IntPtr hCurrentEditView = GetCurrentScintilla();
                    int    currentPos       = (int)Win32.SendMessage(hCurrentEditView, SciMsg.SCI_GETCURRENTPOS, 0, 0);
                    int    beginPos         = currentPos - (bufCapacity - 1);
                    int    startPos         = (beginPos > 0) ? beginPos : 0;
                    int    size             = currentPos - startPos;

                    if (size >= 3)
                    {
                        using (Sci_TextRange tr = new Sci_TextRange(startPos, currentPos, bufCapacity))
                        {
                            Win32.SendMessage(hCurrentEditView, SciMsg.SCI_GETTEXTRANGE, 0, tr.NativePointer);
                            string buf = tr.lpstrText;

                            if (buf[size - 2] != '/')
                            {
                                StringBuilder insertString = new StringBuilder("</");

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

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

                                    Regex regex = new Regex(@"[\._\-:\w]");
                                    while (regex.IsMatch(buf[pCur].ToString()))
                                    {
                                        insertString.Append(buf[pCur]);
                                        pCur++;
                                    }
                                    insertString.Append('>');

                                    if (insertString.Length > 3)
                                    {
                                        Win32.SendMessage(hCurrentEditView, SciMsg.SCI_BEGINUNDOACTION, 0, 0);
                                        Win32.SendMessage(hCurrentEditView, SciMsg.SCI_REPLACESEL, 0, insertString);
                                        Win32.SendMessage(hCurrentEditView, SciMsg.SCI_SETSEL, currentPos, currentPos);
                                        Win32.SendMessage(hCurrentEditView, SciMsg.SCI_ENDUNDOACTION, 0, 0);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }