예제 #1
0
 /// <summary>
 ///  Reads a maximum of count characters from the current stream and writes the data to buffer, beginning at index.
 /// </summary>
 /// <param name="buffer">The buffer to receive the characters.</param>
 /// <param name="index">The position in buffer at which to begin writing.</param>
 /// <param name="count">The maximum number of characters to read.</param>
 /// <returns>The actual number of characters that have been read. The number will be less than or equal to count.</returns>
 /// <exception cref="System.ArgumentNullException">buffer is null.</exception>
 /// <exception cref="System.ArgumentOutOfRangeException">The buffer length minus index is less than count.</exception>
 /// <exception cref="System.ArgumentOutOfRangeException">index or count is negative.</exception>
 public override int ReadBlock(char[] buffer, int index, int count)
 {
     if (_data != null)
     {
         var bufferRemaining = BufferRemaining;
         if (count < bufferRemaining)
         {
             // buffer larger than read size
             _data.CopyTo(_dataIndex, buffer, index, count);
             return(count);
         }
         else
         {
             // buffer smaller or equal to read size
             _data.CopyTo(_dataIndex, buffer, index, bufferRemaining);
             if (count > bufferRemaining)
             {
                 // buffer is smaller; read rest
                 var rest = _scintilla.GetTextRange(
                     _nextData,
                     Math.Min(count - bufferRemaining, UnbufferedRemaining));
                 rest.CopyTo(0, buffer, index + bufferRemaining, rest.Length);
                 count      = bufferRemaining + rest.Length;
                 _nextData += rest.Length;
             }
             // read at least up to buffer's end; refill buffer
             BufferNextRegion();
             return(count);
         }
     }
     else
     {
         return(0);
     }
 }
        private void Lowercase()
        {
            // save the selection
            int start = _textArea.SelectionStart;
            int end   = _textArea.SelectionEnd;

            // modify the selected text
            _textArea.ReplaceSelection(_textArea.GetTextRange(start, end - start).ToLower());

            // preserve the original selection
            _textArea.SetSelection(start, end);
        }
        private void Lowercase()
        {
            // save the selection
            int start = TextArea.SelectionStart;
            int end   = TextArea.SelectionEnd;

            // modify the selected text
            TextArea.ReplaceSelection(TextArea.GetTextRange(start, end - start).ToLower());

            // preserve the original selection
            TextArea.SetSelection(start, end);
            addRecentCommand(LOWERCASE);
        }
예제 #4
0
        private void ContextMenu_Popup(System.Object sender, System.EventArgs e)
        {
            scintilla1.ContextMenu.MenuItems.Clear();

            scintilla1.ContextMenu.MenuItems.Add(new MenuItem("Copy", (s, ea) => scintilla1.Copy()));
            scintilla1.ContextMenu.MenuItems.Add(new MenuItem("Select All", (s, ea) => scintilla1.SelectAll()));



            System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
            var cor = scintilla1.PointToClient(point);
            var pos = scintilla1.CharPositionFromPoint(cor.X, cor.Y);

            int startPos = ValueStartPosition(pos);
            int endPos   = ValueEndPosition(pos);

            string text = scintilla1.GetTextRange(startPos, endPos - startPos);

            if ((text.StartsWith("http://") || text.StartsWith("https://")))
            {
                scintilla1.ContextMenu.MenuItems.Add("-");
                scintilla1.ContextMenu.MenuItems.Add(new MenuItem("Open in Browser", (s, ea) => Process.Start(text)));
                if (text.EndsWith(".jpg") || text.EndsWith(".png"))
                {
                    scintilla1.ContextMenu.MenuItems.Add(new MenuItem("Open Image in new Window", (s, ea) =>
                    {
                        new ImageViewer(text).ShowDialog();
                    }));
                    scintilla1.ContextMenu.MenuItems.Add(new MenuItem("Save Image", (s, ea) =>
                    {
                        SaveFileDialog saveFileDialog1   = new SaveFileDialog();
                        saveFileDialog1.RestoreDirectory = true;
                        saveFileDialog1.FileName         = text.Split('/').Last();
                        saveFileDialog1.Filter           = "Image Files|*." + text.Split('.').Last();
                        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            using (var webClient = new WebClient())
                            {
                                webClient.DownloadFile(text, saveFileDialog1.FileName);
                            }
                        }
                    }));
                }
            }
        }
예제 #5
0
        private string GetUrlAtPosition(int position)
        {
            // Determine whether the specified position is on our 'URL indicator'
            // and if so whether it is a valid URL.

            var urlIndicator    = m_rScintilla_TextArea.Indicators[0];
            var bitmapFlag      = (1 << urlIndicator.Index);
            var bitmap          = m_rScintilla_TextArea.IndicatorAllOnFor(position);
            var hasUrlIndicator = ((bitmapFlag & bitmap) == bitmapFlag);

            if (hasUrlIndicator)
            {
                var startPos = urlIndicator.Start(position);
                var endPos   = urlIndicator.End(position);

                var text = m_rScintilla_TextArea.GetTextRange(startPos, endPos - startPos).Trim();
                if (Uri.IsWellFormedUriString(text, UriKind.Absolute))
                {
                    return(text);
                }
            }

            return(null);
        }
예제 #6
0
        /// <summary>
        /// Get the current cursor at the forward word
        /// </summary>
        /// <returns></returns>
        public string GetCurrentWord()
        {
            int currentPos = TextArea.CurrentPosition;
            int size       = 64;
            int beg        = currentPos - (size - 1);

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

            size = end - beg;
            String txtRange = TextArea.GetTextRange(beg, size);

            string[] arr = txtRange.Split(_Spliter);
            return(arr[arr.Length - 1]);
        }
예제 #7
0
        public void Style(Scintilla scintilla, int startPos, int endPos)
        {
            // 改行コードをWindowsフォーマット(CR+LF)またはUnixフォーマット(LF)から
            // Macフォーマット(CR)にしたときに発生する例外(IndexOutOfRangeException)を回避
            startPos = startPos < 0 ? 0 : startPos;
            endPos = endPos > scintilla.TextLength ? scintilla.TextLength : endPos;

            int lineNumber = scintilla.LineFromPosition(startPos);
            startPos = scintilla.Lines[lineNumber].Position;

            char chBeforePrev = '\0';
            char chPrev = '\0';
            char ch = '\0';
            char chNext = '\0';
            char chAfterNext = '\0';
            int length = 0;
            ParseState state = ParseState.None;

            switch (scintilla.GetStyleAt(startPos - 1)) {
                case HspLexer.StylePreprocessor:
                    state = ParseState.AfterPreprocessor;
                    break;
                case HspLexer.StyleString:
                    state = ParseState.String;
                    break;
                case HspLexer.StyleComment:
                    state = ParseState.Comment;
                    break;
            }

            scintilla.StartStyling(startPos);

            int currentPos = startPos;
            int currentLine = lineNumber;
            int currentLineStartPos = startPos;
            int currentLineEndPos = scintilla.Lines[lineNumber].EndPosition;

            while (currentPos < endPos) {
                chBeforePrev = (char)scintilla.GetCharAt(currentPos - 2);
                chPrev = (char)scintilla.GetCharAt(currentPos - 1);
                ch = (char)scintilla.GetCharAt(currentPos);
                chNext = (char)scintilla.GetCharAt(currentPos + 1);
                chAfterNext = (char)scintilla.GetCharAt(currentPos + 2);

            Process:
                switch (state) {
                    case ParseState.None:
                        if (ch == '\"') {
                            // 文字列
                            scintilla.SetStyling(1, HspLexer.StyleStringLine);
                            state = ParseState.StringLine;
                        } else if (ch == '{' && chNext == '\"') {
                            // 複数行文字列
                            scintilla.SetStyling(2, HspLexer.StyleString);
                            state = ParseState.String;
                            ++currentPos;
                        } else if (ch == '\'') {
                            // 文字
                            scintilla.SetStyling(1, HspLexer.StyleCharacter);
                            state = ParseState.Character;
                        } else if (ch == ';') {
                            // 単一行コメント
                            scintilla.SetStyling(1, HspLexer.StyleCommentLine);
                            state = ParseState.CommentLine;
                        } else if (ch == '/' && chNext == '/') {
                            // 単一行コメント
                            scintilla.SetStyling(2, HspLexer.StyleCommentLine);
                            state = ParseState.CommentLine;
                            ++currentPos;
                        } else if (ch == '/' && chNext == '*') {
                            // 複数行コメント
                            scintilla.SetStyling(2, HspLexer.StyleComment);
                            state = ParseState.Comment;
                            ++currentPos;
                        } else if (ch == '*' && (Char.IsLetter(chNext) || chNext == '_') &&
                            String.IsNullOrWhiteSpace(scintilla.GetTextRange(
                                currentLineStartPos, currentPos - currentLineStartPos))) {
                            // ラベル
                            scintilla.SetStyling(2, HspLexer.StyleLabel);
                            state = ParseState.Label;
                            ++currentPos;
                        } else if (ch == '#' && length == 0 &&
                            String.IsNullOrWhiteSpace(scintilla.GetTextRange(
                                currentLineStartPos, currentPos - currentLineStartPos))) {
                            // プリプロセッサ
                            state = ParseState.Preprocessor;
                            goto Process;
                        } else if ((Char.IsLetter(ch) || ch == '_') && length == 0) {
                            // 識別子
                            state = ParseState.Identifier;
                            goto Process;
                        } else if (ch == ':') {
                            // 行区切り
                            currentLineStartPos = currentPos + 1;
                            scintilla.SetStyling(1, HspLexer.StyleDefault);
                        } else if (ch == '\0') {
                            // NULL文字の場合はすぐreturn
                            return;
                        } else {
                            scintilla.SetStyling(1, HspLexer.StyleDefault);
                        }
                        break;

                    case ParseState.StringLine:
                        if (ch == '\\' && chNext == '\\') {
                            // エスケープシーケンス '\\'
                            scintilla.SetStyling(2, HspLexer.StyleStringLine);
                            ++currentPos;
                        } else if (ch == '\\' && chNext == '\"') {
                            // エスケープシーケンス '\"'
                            scintilla.SetStyling(2, HspLexer.StyleStringLine);
                            ++currentPos;
                        } else if (ch == '\"') {
                            scintilla.SetStyling(1, HspLexer.StyleStringLine);
                            state = ParseState.None;
                        } else {
                            scintilla.SetStyling(1, HspLexer.StyleStringLine);
                        }
                        break;

                    case ParseState.String:
                        if (ch == '\\' && chNext == '\\') {
                            // エスケープシーケンス '\\'
                            scintilla.SetStyling(2, HspLexer.StyleString);
                            ++currentPos;
                        } else if (ch == '\\' && chNext == '\"') {
                            // エスケープシーケンス '\"'
                            scintilla.SetStyling(2, HspLexer.StyleString);
                            ++currentPos;
                        } else if (ch == '\"' && chNext == '}') {
                            scintilla.SetStyling(2, HspLexer.StyleString);
                            state = ParseState.None;
                            ++currentPos;
                        } else {
                            scintilla.SetStyling(1, HspLexer.StyleString);
                        }
                        break;

                    case ParseState.Character:
                        if (ch == '\'') {
                            scintilla.SetStyling(1, HspLexer.StyleCharacter);
                            state = ParseState.None;
                        } else {
                            scintilla.SetStyling(1, HspLexer.StyleCharacter);
                        }
                        break;

                    case ParseState.CommentLine:
                        if (ch == '\n') {
                            scintilla.SetStyling(1, HspLexer.StyleCommentLine);
                            state = ParseState.None;
                        } else {
                            scintilla.SetStyling(1, HspLexer.StyleCommentLine);
                        }
                        break;

                    case ParseState.Comment:
                        if (ch == '*' && chNext == '/') {
                            scintilla.SetStyling(2, HspLexer.StyleComment);
                            state = ParseState.None;
                            ++currentPos;
                        } else {
                            scintilla.SetStyling(1, HspLexer.StyleComment);
                        }
                        break;

                    case ParseState.Label:
                        if (Char.IsLetterOrDigit(ch) || ch == '_') {
                            scintilla.SetStyling(1, HspLexer.StyleLabel);
                        } else {
                            state = ParseState.None;
                            goto Process;
                        }
                        break;

                    case ParseState.Preprocessor:
                        if (ch == '#' && length == 0 &&
                            String.IsNullOrWhiteSpace(scintilla.GetTextRange(
                                currentLineStartPos, currentPos - currentLineStartPos))) {
                            ++length;
                        } else if ((ch == '\t' || ch == '\u0020' || ch == '\u3000') && length == 1) {
                            // '#' の後のタブ文字, 半角スペース, 全角スペースの場合
                            state = ParseState.SpaceInPreprocessor;
                            goto Process;
                        } else if ((Char.IsLetter(ch) || ch == '_') && length == 1) {
                            ++length;
                        } else if ((Char.IsLetterOrDigit(ch) || ch == '_') && length > 1) {
                            ++length;
                        } else {
                            int style = HspLexer.StyleDefault;
                            string identifier = scintilla.GetTextRange(currentPos - length, length);

                            char[] removeChars = { '\t', '\u0020', '\u3000' };
                            identifier = removeChars.Aggregate(identifier, (s, c) => s.Replace(c.ToString(), String.Empty));
                            identifier = identifier.ToLower();

                            if (this.mKeywords.ContainsKey(identifier)) {
                                switch (this.mKeywords[identifier].mType) {
                                    case KeywordType.Preprocessor:
                                        style = HspLexer.StylePreprocessor;
                                        break;
                                }
                            }

                            scintilla.SetStyling(length, style);
                            length = 0;

                            switch (style) {
                                case HspLexer.StyleDefault:
                                    state = ParseState.None;
                                    break;
                                case HspLexer.StylePreprocessor:
                                    state = ParseState.AfterPreprocessor;
                                    break;
                            }
                            goto Process;
                        }
                        break;

                    case ParseState.SpaceInPreprocessor:
                        if ((ch == '\t' || ch == '\u0020' || ch == '\u3000')) {
                            ++length;
                        } else if ((Char.IsLetter(ch) || ch == '_')) {
                            state = ParseState.Preprocessor;
                            goto Process;
                        } else {
                            scintilla.SetStyling(length, HspLexer.StyleDefault);
                            length = 0;
                            state = ParseState.None;
                            goto Process;
                        }
                        break;

                    case ParseState.AfterPreprocessor:
                        if (ch == '\\' && chNext == '\r' && chAfterNext == '\n') {
                            scintilla.SetStyling(3, HspLexer.StylePreprocessor);
                            currentPos += 2;
                        } else if (ch == '\\' && chNext == '\n') {
                            scintilla.SetStyling(2, HspLexer.StylePreprocessor);
                            ++currentPos;
                        } else if (ch == '\r' && chNext == '\n') {
                            scintilla.SetStyling(2, HspLexer.StyleDefault);
                            state = ParseState.None;
                            ++currentPos;
                        } else if (ch == '\n') {
                            scintilla.SetStyling(1, HspLexer.StyleDefault);
                            state = ParseState.None;
                        } else {
                            scintilla.SetStyling(1, HspLexer.StylePreprocessor);
                        }
                        break;

                    case ParseState.Identifier:
                        if (Char.IsLetterOrDigit(ch) || ch == '_') {
                            ++length;
                        } else {
                            int style = HspLexer.StyleDefault;
                            string identifier = scintilla.GetTextRange(currentPos - length, length);
                            identifier = identifier.ToLower();

                            if (this.mKeywords.ContainsKey(identifier)) {
                                switch (this.mKeywords[identifier].mType) {
                                    case KeywordType.Macro:
                                        style = HspLexer.StyleMacro;
                                        break;
                                    case KeywordType.Function:
                                        style = HspLexer.StyleFunction;
                                        break;
                                    case KeywordType.Type:
                                        style = HspLexer.StyleType;
                                        break;
                                }
                            }

                            scintilla.SetStyling(length, style);
                            length = 0;
                            state = ParseState.None;
                            goto Process;
                        }
                        break;
                }

                ++currentPos;
                currentLine = scintilla.LineFromPosition(currentPos);

                if (currentLineStartPos < scintilla.Lines[currentLine].Position) {
                    currentLineStartPos = scintilla.Lines[currentLine].Position;
                }

                currentLineEndPos = scintilla.Lines[currentLine].EndPosition;
            }
        }
        public void StyleText(Scintilla scintilla, int startPos, int endPos)
        {
            if(scintilla.GetCharAt(0) != '=') //Not Expression
                return;

            // Back up to the line start
            var line = scintilla.LineFromPosition(startPos);
            startPos = scintilla.Lines[line].Position;

            var length = 0;
            char stringStartChar = '"';
            var state = LexerState.Unknown;
            var EOF = false;

            // Start styling
            scintilla.StartStyling(startPos);
            while (startPos < endPos)
            {
                var c = (char)scintilla.GetCharAt(startPos);
                //lastSymbol = startPos == endPos - 1;

            REPROCESS:
                switch (state)
                {
                    case LexerState.Unknown:
                        if (c == '"' || c == '\'')
                        {
                            // Start of "string"
                            stringStartChar = c;
                            scintilla.SetStyling(1, (int)Style.String);
                            state = LexerState.String;
                        }
                        else if (c == '{')
                        {
                            state = LexerState.Braces;
                            goto REPROCESS;
                        }
                        else if (Char.IsDigit(c))
                        {
                            state = LexerState.Number;
                            goto REPROCESS;
                        }
                        else if (Char.IsLetter(c))
                        {
                            state = LexerState.Identifier;
                            goto REPROCESS;
                        }
                        else if (operators.Any(x => x[0] == c))
                        {
                            state = LexerState.Operator;
                            goto REPROCESS;
                        }
                        else
                        {
                            // Everything else
                            scintilla.SetStyling(1, (int)Style.Default);
                        }
                        break;

                    case LexerState.String:
                        if (c == stringStartChar)
                        {
                            length++;
                            scintilla.SetStyling(length, (int)Style.String);
                            length = 0;
                            state = LexerState.Unknown;
                        }
                        else
                        {
                            length++;
                        }
                        break;

                    case LexerState.Braces:
                        if (c == '}')
                        {
                            length++;
                            var style = Style.Identifier;
                            var identifier = scintilla.GetTextRange(startPos - length + 2, length - 2);
                            if (identifier.Length == 0)
                            {
                                style = Style.Error;
                            }
                            else if (identifier[0] == '!')
                            {
                                if (userInfo.Contains(identifier.Substring(1)))
                                    style = Style.UserInfo;
                                else
                                    style = Style.Error;
                            }
                            else if (identifier[0] == '@')
                            {
                                if (globals.Contains(identifier.Substring(1)))
                                    style = Style.Globals;
                                else
                                    style = Style.Error;
                            }
                            else if (identifier[0] == '?' && parameters != null)
                            {
                                if (parameters.Contains(identifier.Substring(1)))
                                    style = Style.Parameter;
                                else
                                    style = Style.Error;
                            }
                            else
                            {
                                if (fields.Contains(identifier))
                                    style = Style.Field;
                                else
                                    style = Style.Error;
                            }

                            scintilla.SetStyling(length, (int)style);
                            length = 0;
                            state = LexerState.Unknown;
                        }
                        else
                            length++;
                        break;

                    case LexerState.Number:
                        if (Char.IsDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || c == 'x')
                        {
                            length++;
                        }
                        else
                        {
                            scintilla.SetStyling(length, (int)Style.Number);
                            length = 0;
                            state = LexerState.Unknown;
                            goto REPROCESS;
                        }
                        break;

                    case LexerState.Identifier:
                        if (!EOF && (Char.IsLetterOrDigit(c) || c == '.' || c == '!' || c == '_'))
                        {
                            length++;
                        }
                        else
                        {
                            var style = Style.Identifier;
                            var identifier = scintilla.GetTextRange(startPos - length, length);

                            var endFirstWord = identifier.IndexOf('.');
                            if (endFirstWord != -1)
                            {
                                var firstWord = identifier.Substring(0, endFirstWord);
                                var secondWord = identifier.Substring(endFirstWord + 1);
                                if (calssMethods.ContainsKey(firstWord) && calssMethods[firstWord].Contains(secondWord))
                                {
                                    style = Style.Method;
                                }
                                else
                                    style = Style.Error;
                            }

                            endFirstWord = identifier.IndexOf('!');
                            if (endFirstWord != -1)
                            {
                                var firstWord = identifier.Substring(0, endFirstWord);
                                var secondWord = identifier.Substring(endFirstWord +1);
                                if (firstWord == "User")
                                {
                                    if (userInfo.Contains(secondWord))
                                        style = Style.UserInfo;
                                    else
                                        style = Style.Error;
                                }
                                if (firstWord == "Globals")
                                {
                                    if (globals.Contains(secondWord))
                                        style = Style.Globals;
                                    else
                                        style = Style.Error;
                                }
                                if (firstWord == "Parameters")
                                {
                                    if (globals.Contains(secondWord))
                                        style = Style.Parameter;
                                    else
                                        style = Style.Error;
                                }
                                if (firstWord == "Fields")
                                {
                                    var field = secondWord.Split('.');
                                    if (field.Length == 2 && fields.Contains(field[0])
                                        && (field[1] == "Value" || field[1] == "IsMissing"))
                                        style = Style.Field;
                                    else
                                        style = Style.Error;
                                }

                            }

                            if (simpleMethods.Contains(identifier))
                                style = Style.Method;

                            if (aggrMethods.Contains(identifier))
                                style = Style.AggrMethod;

                            scintilla.SetStyling(length, (int)style);
                            length = 0;
                            state = LexerState.Unknown;
                            if(!EOF)
                                goto REPROCESS;
                        }
                        break;
                    case LexerState.Operator:
                        var cur = scintilla.GetTextRange(startPos - length, length +1);
                        if (operators.Any(x => x.StartsWith(cur)))
                        {
                            length++;
                        }
                        else
                        {
                            cur = scintilla.GetTextRange(startPos - length, length);
                            Style style;
                            if (operators.Contains(cur))
                            {
                                //length++;
                                style = Style.Operator;
                            }
                            else
                            {
                                style = Style.Error;
                            }
                            scintilla.SetStyling(length, (int)style);
                            length = 0;
                            state = LexerState.Unknown;
                            goto REPROCESS;
                        }
                        break;
                }

                startPos++;
                if (!EOF && startPos == endPos && state == LexerState.Identifier)
                {
                    EOF = true;
                    goto REPROCESS;
                }

            }
        }