private int ValueStartPosition(int position) { bool found = false; int currentposition = position; while (!found) { if (currentposition < 0) { found = true; } else { if (scintilla1.GetCharAt(currentposition) == '"') { found = true; currentposition += 1; } else { currentposition -= 1; } } } return(currentposition); }
/// <summary> /// manages Brace highlighting /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void TextArea_UpdateUI(object sender, UpdateUIEventArgs e) { // Has the caret changed position? var caretPos = TextArea.CurrentPosition; if (lastCaretPos != caretPos) { UpdateStatusEventArgs evt = new UpdateStatusEventArgs(); evt.Text = "Pos: " + caretPos.ToString(); FireUpdateStatus(this, evt); lastCaretPos = caretPos; var bracePos1 = -1; var bracePos2 = -1; //TODO Brace higligthing not working or overridden by Lexer?? // Is there a brace to the left or right? if (caretPos > 0 && IsBrace(TextArea.GetCharAt(caretPos - 1))) { bracePos1 = (caretPos - 1); } else if (IsBrace(TextArea.GetCharAt(caretPos))) { bracePos1 = caretPos; } if (bracePos1 >= 0) { // Find the matching brace bracePos2 = TextArea.BraceMatch(bracePos1); if (bracePos2 == Scintilla.InvalidPosition) { TextArea.BraceBadLight(bracePos1); TextArea.HighlightGuide = 0; } else { TextArea.BraceHighlight(bracePos1, bracePos2); TextArea.HighlightGuide = TextArea.GetColumn(bracePos1); } } else { // Turn off brace matching TextArea.BraceHighlight(Scintilla.InvalidPosition, Scintilla.InvalidPosition); TextArea.HighlightGuide = 0; } } }
private void scintilla_UpdateUI(object sender, UpdateUIEventArgs e) { // Has the caret changed position? var caretPos = scintilla1.CurrentPosition; if (lastCaretPos != caretPos) { lastCaretPos = caretPos; var bracePos1 = -1; var bracePos2 = -1; // Is there a brace to the left or right? if (caretPos > 0 && IsBrace(scintilla1.GetCharAt(caretPos - 1))) { bracePos1 = (caretPos - 1); } else if (IsBrace(scintilla1.GetCharAt(caretPos))) { bracePos1 = caretPos; } if (bracePos1 >= 0) { // Find the matching brace bracePos2 = scintilla1.BraceMatch(bracePos1); if (bracePos2 == ScintillaNET.Scintilla.InvalidPosition) { scintilla1.BraceBadLight(bracePos1); } else { scintilla1.BraceHighlight(bracePos1, bracePos2); } } else { // Turn off brace matching scintilla1.BraceHighlight(ScintillaNET.Scintilla.InvalidPosition, ScintillaNET.Scintilla.InvalidPosition); } } }
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; } }
//stolen from https://github.com/jacobslusser/ScintillaNET/wiki/Character-Autocompletion#finishing-touch private void InsertMatchedChars(CharAddedEventArgs e) { var caretPos = _codeArea.CurrentPosition; var docStart = caretPos == 1; var docEnd = caretPos == _codeArea.Text.Length; var charPrev = docStart ? _codeArea.GetCharAt(caretPos) : _codeArea.GetCharAt(caretPos - 2); var charNext = _codeArea.GetCharAt(caretPos); var isCharPrevBlank = charPrev == ' ' || charPrev == '\t' || charPrev == '\n' || charPrev == '\r'; var isCharNextBlank = charNext == ' ' || charNext == '\t' || charNext == '\n' || charNext == '\r' || docEnd; var isEnclosed = (charPrev == '(' && charNext == ')') || (charPrev == '{' && charNext == '}') || (charPrev == '[' && charNext == ']'); var isSpaceEnclosed = (charPrev == '(' && isCharNextBlank) || (isCharPrevBlank && charNext == ')') || (charPrev == '{' && isCharNextBlank) || (isCharPrevBlank && charNext == '}') || (charPrev == '[' && isCharNextBlank) || (isCharPrevBlank && charNext == ']'); var isCharOrString = (isCharPrevBlank && isCharNextBlank) || isEnclosed || isSpaceEnclosed; var charNextIsCharOrString = charNext == '"' || charNext == '\''; switch (e.Char) { case '(': if (charNextIsCharOrString) { return; } _codeArea.InsertText(caretPos, ")"); break; case '{': if (charNextIsCharOrString) { return; } _codeArea.InsertText(caretPos, "}"); break; case '[': if (charNextIsCharOrString) { return; } _codeArea.InsertText(caretPos, "]"); break; case '"': // 0x22 = " if (charPrev == 0x22 && charNext == 0x22) { _codeArea.DeleteRange(caretPos, 1); _codeArea.GotoPosition(caretPos); return; } if (isCharOrString) { _codeArea.InsertText(caretPos, "\""); } break; case '\'': // 0x27 = ' if (charPrev == 0x27 && charNext == 0x27) { _codeArea.DeleteRange(caretPos, 1); _codeArea.GotoPosition(caretPos); return; } if (isCharOrString) { _codeArea.InsertText(caretPos, "'"); } break; } }
/// <summary> /// Show a tooltip with information about the entered object method or property. /// </summary> /// <param name="scintilla"></param> /// <param name="reader">Jolt's XmlDocCommentReader instance, to get and display comments from assembly-generated XML file.</param> /// <remarks></remarks> public static void ShowToolTip(this ScintillaNET.Scintilla scintilla, Jolt.XmlDocCommentReader reader) { //parses the last keyword (object) (before the ".") and get suggestions for the autocomplete box from its properties and methods string [] splitters = { ".", "(", ")", " ", "\r", "\n", "\r\n" }; dynamic text = ScintillaExtender.getLastWord(scintilla).Split(splitters, StringSplitOptions.RemoveEmptyEntries); dynamic lastchar = Convert.ToChar(scintilla.GetCharAt(scintilla.CurrentPosition)); string helptext = ""; if (text.Length >= 2) { string lastkeyword = text[text.Length - 1]; string lastobj = text[text.Length - 2].Trim(); //string obj = Convert.ToString(lastobj)); switch (lastobj) { case "Browser": dynamic prop = typeof(TopDown_QA_FrameWork.Driver).GetMember(lastkeyword); if (prop.Length > 0) { helptext = ScintillaExtender.FormatHelpTip(scintilla, prop[0], reader); } break; case "ims1": case "ims2": case "ims3": case "ims4": case "ims5": case "ims6": case "oms1": case "oms2": case "oms3": case "oms4": case "oms5": case "MaterialStream": prop = Type.GetType("DWSIM.DWSIM.SimulationObjects.Streams.MaterialStream").GetMember(lastkeyword); if (prop.Length > 0) { helptext = ScintillaExtender.FormatHelpTip(scintilla, prop[0], reader); } break; case "ies1": case "oes1": case "EnergyStream": prop = Type.GetType("DWSIM.DWSIM.SimulationObjects.Streams.EnergyStream").GetMember(lastkeyword); if (prop.Length > 0) { helptext = ScintillaExtender.FormatHelpTip(scintilla, prop[0], reader); } break; case "Flowsheet": prop = Type.GetType("DWSIM.FormFlowsheet").GetMember(lastkeyword); if (prop.Length > 0) { helptext = ScintillaExtender.FormatHelpTip(scintilla, prop[0], reader); } break; case "Spreadsheet": prop = Type.GetType("DWSIM.SpreadsheetForm").GetMember(lastkeyword); if (prop.Length > 0) { helptext = ScintillaExtender.FormatHelpTip(scintilla, prop[0], reader); } break; case "PropertyPackage": prop = Type.GetType("DWSIM.DWSIM.SimulationObjects.PropertyPackages.PropertyPackage").GetMember(lastkeyword); if (prop.Length > 0) { helptext = ScintillaExtender.FormatHelpTip(scintilla, prop[0], reader); } break; case "UnitOp": case "Me": prop = Type.GetType("DWSIM.SimulationObjects_UnitOpBaseClass").GetMember(lastkeyword); if (prop.Length > 0) { helptext = ScintillaExtender.FormatHelpTip(scintilla, prop(0), reader); } break; case "Solver": prop = Type.GetType("DWSIM.DWSIM.Flowsheet.FlowsheetSolver").GetMember(lastkeyword); if (prop.Length > 0) { helptext = ScintillaExtender.FormatHelpTip(scintilla, prop(0), reader); } break; } //shows the tooltip if (!string.IsNullOrEmpty(helptext)) { scintilla.CallTipShow(scintilla.CurrentPosition, helptext); } else { scintilla.CallTipCancel(); } } else { //hides tooltip if visible scintilla.CallTipCancel(); } }
/// <summary> /// Show an autocomplete listbox with methods and properties from the object with the entered keyword. /// </summary> /// <param name="scintilla"></param> /// <remarks></remarks> public static void ShowAutoComplete(this ScintillaNET.Scintilla scintilla) { string suggestions = ""; //parses the last keyword (object) (before the ".") and get suggestions for the autocomplete box from its properties and methods string [] splitters = { ".", "(", ")", " ", "\r", "\n", "\r\n" }; dynamic text = ScintillaExtender.getLastWord(scintilla).Split(splitters, StringSplitOptions.RemoveEmptyEntries); dynamic lastchar = Convert.ToChar(scintilla.GetCharAt(scintilla.CurrentPosition)); //text2 = text2.Remove(text2.Length - 2).Trim(); string lastkeyword = ""; if (text.Length >= 1) { if (text.Length >= 2) { lastkeyword = text[text.Length - 2].Trim(); // System.Windows.MessageBox.Show("[1] " + lastkeyword); } else { lastkeyword = text[text.Length - 1].Trim(); // System.Windows.MessageBox.Show("[2] " + lastkeyword); } switch (lastkeyword) { case "Browser": dynamic props = typeof(TopDown_QA_FrameWork.Driver).GetProperties(); foreach (var p_loopVariable in props) { //System.Windows.Forms.MessageBox.Show(p_loopVariable.Name); var p = p_loopVariable; suggestions += (p.Name) + " "; } dynamic methods = typeof(TopDown_QA_FrameWork.Driver).GetMethods(); foreach (var m_loopVariable in methods) { var m = m_loopVariable; suggestions += (m.Name) + " "; } break; default: if ((Convert.ToInt32(scintilla.Tag)) == 1) { suggestions = "Browser teste1"; } else { suggestions = "Browser teste2"; } break; } } else { if (Convert.ToInt32(scintilla.Tag) == 1) { //editor is being used at flowsheet level. suggestions = "Browser teste3"; } else { //editor is being used at script unit operation level suggestions = "Browser teste4 solver zinbu"; } } dynamic currentPos = scintilla.CurrentPosition; dynamic wordStartPos = scintilla.WordStartPosition(currentPos, true); // Display the autocompletion list dynamic lenEntered = currentPos - wordStartPos; //System.Windows.Forms.MessageBox.Show(lenEntered.ToString() + " lastChar= " + lastchar ); //System.Windows.Forms.MessageBox.Show(suggestions); //System.Windows.Forms.MessageBox.Show(lastkeyword.ToString()); if (lenEntered > 0 & lastchar != '.') { scintilla.AutoCShow(lenEntered, suggestions); } else { if (lastchar == '.' & text.Length >= 1) { //System.Windows.Forms.MessageBox.Show(suggestions); scintilla.AutoCShow(0, suggestions.Trim()); } else { //System.Windows.Forms.MessageBox.Show("lenEntered: " + lenEntered.ToString() + "; lastChar: " + lastchar.ToString() + "; text.length: " + text.Length.ToString()); } } }
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; } } }