Exemple #1
0
        static public string GetTextBetween(this ScintillaGateway document, int start, int end = -1)
        {
            if (end == -1)
            {
                end = document.GetLength();
            }

            return(document.get_text_range(start, end, end - start + 1)); //+1 for null termination
        }
Exemple #2
0
        /// <summary>
        /// Find and capitalize the first letter of the beginning of all
        /// sentences in a document.
        /// </summary>
        internal static void CapSentence()
        {
            // Get current scintilla instance.
            IntPtr           currentScint     = PluginBase.GetCurrentScintilla();
            ScintillaGateway scintillaGateway = new ScintillaGateway(currentScint);

            try
            {
                // Get the length of the document.
                int length = scintillaGateway.GetLength();
                // Get the text in the document.
                string allText = scintillaGateway.GetText(length + 1);

                // Convert the text to char array for easy manipulation.
                char[] charArrayAllText = allText.ToCharArray();

                char firstLetter   = new char();
                bool capAfterPunct = true;

                // For the length of the selected text...
                for (int i = 0; i < allText.Length; i++)
                {
                    // If there is punctuation that ends a sentence
                    // the next word should be capitalized.
                    if (allText[i] == '.' || allText[i] == '?' || allText[i] == '!' || allText[i] == '\r')
                    {
                        capAfterPunct = true;
                    }
                    // Don't capitalize Markdown titles in this method.
                    else if (allText[i] == '#')
                    {
                        capAfterPunct = false;
                    }

                    if (capAfterPunct && !ignoreChars.Contains(allText[i]))
                    {
                        // If the current character is not a whitespace character
                        // convert the first letter of the word to uppercase.
                        firstLetter = Convert.ToChar(allText[i].ToString().ToUpperInvariant());
                        // Replace the correct char in the selected text with its uppercase letter.
                        charArrayAllText.SetValue(firstLetter, i);
                        // Revert to false until beginning of next sentence.
                        capAfterPunct = false;
                    }
                }
                // Convert char array back to string.
                allText = new string(charArrayAllText);
                // Replace the document text with the new text.
                scintillaGateway.SelectAll();
                scintillaGateway.ReplaceSel(allText);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        static public void ClearIndicator(this ScintillaGateway document, int indicator, int startPos, int endPos = -1)
        {
            if (endPos == -1)
            {
                endPos = document.GetLength();
            }

            document.SetIndicatorCurrent(indicator);
            document.IndicatorClearRange(startPos, endPos - startPos);
        }
        static public string GetTextBetween(this ScintillaGateway document, int start, int end = -1)
        {
            if (end == -1)
            {
                end = document.GetLength();
            }

            using (var tr = new TextRange(start, end, end - start + 1)) //+1 for null termination
            {
                document.GetTextRange(tr);
                return(tr.lpstrText);
            }
        }
        static public void SetTextBetween(this ScintillaGateway document, string text, int start, int end = -1)
        {
            //supposed not to scroll

            if (end == -1)
            {
                end = document.GetLength();
            }

            document.SetTargetStart(new Position(start));
            document.SetTargetEnd(new Position(end));
            document.ReplaceTarget(text.Length, text);
        }
Exemple #6
0
        static public string TextAfterPosition(this ScintillaGateway document, int position, int maxLength)
        {
            int bufCapacity = maxLength + 1;
            int currentPos  = position;
            int fullLength  = document.GetLength();
            int startPos    = currentPos;
            int endPos      = Math.Min(currentPos + bufCapacity, fullLength);
            int size        = endPos - startPos;

            if (size > 0)
            {
                return(document.get_text_range(startPos, endPos, bufCapacity));
            }
            else
            {
                return(null);
            }
        }
        static public string GetWordAtPosition(this ScintillaGateway document, int position, out Point point, char[] wordDelimiters = null)
        {
            int currentPos = position;
            int fullLength = document.GetLength();

            string leftText  = document.TextBeforePosition(currentPos, 512);
            string rightText = document.TextAfterPosition(currentPos, 512);

            //if updating do not forger to update SimpleCodeCompletion.Delimiters
            var delimiters = "\\·\t\n\r .,:;'\"=[]{}()+-/!?@$%^&*«»><#|~`".ToCharArray();

            if (wordDelimiters != null)
            {
                delimiters = wordDelimiters;
            }

            string wordLeftPart = "";
            int    startPos     = currentPos;

            if (leftText != null)
            {
                bool startOfDoc = leftText.Length == currentPos;
                startPos     = leftText.LastIndexOfAny(delimiters);
                wordLeftPart = (startPos != -1) ? leftText.Substring(startPos + 1) : (startOfDoc ? leftText : "");
                int relativeStartPos = leftText.Length - startPos;
                startPos = (startPos != -1) ? (currentPos - relativeStartPos) + 1 : 0;
            }

            string wordRightPart = "";
            int    endPos        = currentPos;

            if (rightText != null)
            {
                endPos        = rightText.IndexOfAny(delimiters);
                wordRightPart = (endPos != -1) ? rightText.Substring(0, endPos) : "";
                endPos        = (endPos != -1) ? currentPos + endPos : fullLength;
            }

            point = new Point(startPos, endPos);
            return(wordLeftPart + wordRightPart);
        }
        static public string TextAfterPosition(this ScintillaGateway document, int position, int maxLength)
        {
            int bufCapacity = maxLength + 1;
            int currentPos  = position;
            int fullLength  = document.GetLength();
            int startPos    = currentPos;
            int endPos      = Math.Min(currentPos + bufCapacity, fullLength);
            int size        = endPos - startPos;

            if (size > 0)
            {
                using (var tr = new TextRange(startPos, endPos, bufCapacity))
                {
                    document.GetTextRange(tr);
                    return(tr.lpstrText);
                }
            }
            else
            {
                return(null);
            }
        }
Exemple #9
0
        public static string GetAllText(this ScintillaGateway scintillaGateway)
        {
            int length = scintillaGateway.GetLength();

            int    textStride = 10000;
            int    steps      = (int)Math.Ceiling((double)length / textStride);
            string allText    = string.Empty;

            for (int i = 0; i < steps; i++)
            {
                int startIndex = i * textStride;
                int endIndex   = startIndex + textStride - 1;

                if (endIndex >= length)
                {
                    endIndex = length;
                }

                scintillaGateway.SetTargetRange(new Position(startIndex), new Position(endIndex));
                allText += scintillaGateway.GetTargetText();
            }

            return(allText);
        }
Exemple #10
0
 /// <summary>
 /// Selects all.
 /// </summary>
 /// <param name="document">The document.</param>
 static public void SelectAll(this ScintillaGateway document)
 => document.SetSel(0, document.GetLength());
 static public string AllText(this ScintillaGateway document)
 {
     return(document.GetText(document.GetLength() + 10));
 }