/// <summary> /// Checks if a given search match actually points to the given target source /// </summary /// <returns>True if the SearchMatch does point to the target source.</returns> public static bool DoesMatchPointToTarget(ScintillaNet.ScintillaControl Sci, SearchMatch match, ASResult target, DocumentHelper associatedDocumentHelper) { if (Sci == null || target == null) return false; FileModel targetInFile = null; if (target.InFile != null) targetInFile = target.InFile; else if (target.Member != null && target.InClass == null) targetInFile = target.Member.InFile; Boolean matchMember = targetInFile != null && target.Member != null; Boolean matchType = target.Member == null && target.IsStatic && target.Type != null; if (!matchMember && !matchType) return false; ASResult result = null; // get type at match position if (match.Index < Sci.Text.Length) // TODO: find out rare cases of incorrect index reported { result = DeclarationLookupResult(Sci, Sci.MBSafePosition(match.Index) + Sci.MBSafeTextLength(match.Value)); if (associatedDocumentHelper != null) { // because the declaration lookup opens a document, we should register it with the document helper to be closed later associatedDocumentHelper.RegisterLoadedDocument(PluginBase.MainForm.CurrentDocument); } } // check if the result matches the target if (result == null || (result.InFile == null && result.Type == null)) return false; if (matchMember) { if (result.Member == null) return false; var resultInFile = result.InClass != null ? result.InFile : result.Member.InFile; return resultInFile.BasePath == targetInFile.BasePath && resultInFile.FileName == targetInFile.FileName && result.Member.LineFrom == target.Member.LineFrom && result.Member.Name == target.Member.Name; } else // type { if (result.Type == null) return false; if (result.Type.QualifiedName == target.Type.QualifiedName) return true; return false; } }
/// <summary> /// Checks if the given match actually is the declaration. /// </summary> public static bool IsMatchTheTarget(ScintillaNet.ScintillaControl Sci, SearchMatch match, ASResult target) { if (Sci == null || target == null || target.InFile == null || target.Member == null) { return false; } String originalFile = Sci.FileName; // get type at match position ASResult declaration = DeclarationLookupResult(Sci, Sci.MBSafePosition(match.Index) + Sci.MBSafeTextLength(match.Value)); return (declaration.InFile != null && originalFile == declaration.InFile.FileName) && (Sci.CurrentPos == (Sci.MBSafePosition(match.Index) + Sci.MBSafeTextLength(match.Value))); }
public static bool MakePrivate(ScintillaNet.ScintillaControl Sci, MemberModel member) { ContextFeatures features = ASContext.Context.Features; string visibility = GetPrivateKeyword(); if (features.publicKey == null || visibility == null) return false; Regex rePublic = new Regex(String.Format(@"\s*({0})\s+", features.publicKey)); string line; Match m; int index, position; for (int i = member.LineFrom; i <= member.LineTo; i++) { line = Sci.GetLine(i); m = rePublic.Match(line); if (m.Success) { index = Sci.MBSafeTextLength(line.Substring(0, m.Groups[1].Index)); position = Sci.PositionFromLine(i) + index; Sci.SetSel(position, position + features.publicKey.Length); Sci.ReplaceSel(visibility); UpdateLookupPosition(position, features.publicKey.Length - visibility.Length); return true; } } return false; }
public static bool RenameMember(ScintillaNet.ScintillaControl Sci, MemberModel member, string newName) { ContextFeatures features = ASContext.Context.Features; string kind = features.varKey; if ((member.Flags & FlagType.Getter) > 0) kind = features.getKey; else if ((member.Flags & FlagType.Setter) > 0) kind = features.setKey; else if (member.Flags == FlagType.Function) kind = features.functionKey; Regex reMember = new Regex(String.Format(@"{0}\s+({1})[\s:]", kind, member.Name)); string line; Match m; int index, position; for (int i = member.LineFrom; i <= member.LineTo; i++) { line = Sci.GetLine(i); m = reMember.Match(line); if (m.Success) { index = Sci.MBSafeTextLength(line.Substring(0, m.Groups[1].Index)); position = Sci.PositionFromLine(i) + index; Sci.SetSel(position, position + member.Name.Length); Sci.ReplaceSel(newName); UpdateLookupPosition(position, 1); return true; } } return false; }
private static bool RemoveOneLocalDeclaration(ScintillaNet.ScintillaControl Sci, MemberModel contextMember) { string type = ""; if (contextMember.Type != null && (contextMember.Flags & FlagType.Inferred) == 0) { type = FormatType(contextMember.Type); if (type.IndexOf('*') > 0) type = type.Replace("/*", @"/\*\s*").Replace("*/", @"\s*\*/"); type = @":\s*" + type; } Regex reDecl = new Regex(String.Format(@"[\s\(]((var|const)\s+{0}\s*{1})\s*", contextMember.Name, type)); for (int i = contextMember.LineFrom; i <= contextMember.LineTo + 10; i++) { string text = Sci.GetLine(i); Match m = reDecl.Match(text); if (m.Success) { int index = Sci.MBSafeTextLength(text.Substring(0, m.Groups[1].Index)); int position = Sci.PositionFromLine(i) + index; int len = Sci.MBSafeTextLength(m.Groups[1].Value); Sci.SetSel(position, position + len); if (contextMember.Type == null || (contextMember.Flags & FlagType.Inferred) != 0) Sci.ReplaceSel(contextMember.Name + " "); else Sci.ReplaceSel(contextMember.Name); UpdateLookupPosition(position, contextMember.Name.Length - len); return true; } } return false; }
public static string ValidateSource(string fileName, string src, ScintillaNet.ScintillaControl sci) { Stream str = new MemoryStream(Encoding.Default.GetBytes(src)); // model generator AS3Lexer lexer = new AS3Lexer(str); lexer.setFilename(fileName); AS3Parser parser = new AS3Parser(lexer); parser.setFilename(fileName); // start parsing try { parser.compilationUnit(); } catch (RecognitionException rex) { string result = fileName + ":" + rex.line + ": "; string line = (rex.line > 0) ? sci.GetLine(rex.line - 1) : ""; int col = rex.column; if (sci != null && rex.line > 0) { Match token = Regex.Match(rex.Message, "found '([^']+)"); if (!token.Success) token = Regex.Match(rex.Message, "\"([^\"]+)"); // find token position if (token.Success) { string tok = token.Groups[1].Value; int p = line.IndexOf(tok); if (p > 0) { p = sci.MBSafeTextLength(line.Substring(0, p)); int len = sci.MBSafeTextLength(tok); return result + "characters " + p + "-" + (p + len) + " : " + rex.Message; } } // fix column index else { for (int i = 0; i < line.Length; i++) if (line[i] == '\t') col -= 7; else if (line[i] != ' ') break; } } return result + "character "+Math.Max(0, col)+" : "+rex.Message; } catch (TokenStreamRecognitionException trex) { int col = trex.recog.column; if (trex.recog.line > 0) { // fix column index string line = sci.GetLine(trex.recog.line - 1); for (int i = 0; i < line.Length; i++) if (line[i] == '\t') col -= 7; else if (line[i] != ' ') break; } return fileName + ":" + trex.recog.line + ": character " + col + " : " + trex.Message; } catch (TokenStreamException tex) { return fileName + ": IO Error: " + tex.Message; } catch (Exception ex) { return fileName + ": Validator Exception: " + ex.Message; } return null; }
/// <summary> /// Convert multibyte column to byte length /// </summary> private int MBSafeColumn(ScintillaNet.ScintillaControl sci, int line, int length) { String text = sci.GetLine(line) ?? ""; length = Math.Min(length, text.Length); return sci.MBSafeTextLength(text.Substring(0, length)); }
/// <summary> /// Checks if a given search match actually points to the given target source /// </summary /// <returns>True if the SearchMatch does point to the target source.</returns> static public bool DoesMatchPointToTarget(ScintillaNet.ScintillaControl Sci, SearchMatch match, ASResult target, DocumentHelper associatedDocumentHelper) { if (Sci == null || target == null || target.inFile == null || target.Member == null) { return false; } // get type at match position ASResult result = DeclarationLookupResult(Sci, Sci.MBSafePosition(match.Index) + Sci.MBSafeTextLength(match.Value)); if (associatedDocumentHelper != null) { // because the declaration lookup opens a document, we should register it with the document helper to be closed later associatedDocumentHelper.RegisterLoadedDocument(PluginBase.MainForm.CurrentDocument); } // check if the result matches the target // TODO: this method of checking their equality seems pretty crude -- is there a better way? if (result == null || result.inFile == null || result.Member == null) { return false; } Boolean doesMatch = result.inFile.BasePath == target.inFile.BasePath && result.inFile.FileName == target.inFile.FileName && result.Member.LineFrom == target.Member.LineFrom && result.Member.Name == target.Member.Name; return (doesMatch); }
/// <summary> /// Inserts the specified snippet to the document /// </summary> public static Int32 InsertSnippetText(ScintillaNet.ScintillaControl sci, Int32 currentPosition, String snippet) { sci.BeginUndoAction(); try { Int32 newIndent; String text = snippet; if (sci.SelTextSize > 0) currentPosition -= sci.MBSafeTextLength(sci.SelText); Int32 line = sci.LineFromPosition(currentPosition); Int32 indent = sci.GetLineIndentation(line); sci.ReplaceSel(""); Int32 lineMarker = LineEndDetector.DetectNewLineMarker(text, sci.EOLMode); String newline = LineEndDetector.GetNewLineMarker(lineMarker); if (newline != "\n") text = text.Replace(newline, "\n"); newline = LineEndDetector.GetNewLineMarker((Int32)PluginBase.MainForm.Settings.EOLMode); text = PluginBase.MainForm.ProcessArgString(text).Replace(newline, "\n"); newline = LineEndDetector.GetNewLineMarker(sci.EOLMode); String[] splitted = text.Trim().Split('\n'); for (Int32 j = 0; j < splitted.Length; j++) { if (j != splitted.Length - 1) sci.InsertText(sci.CurrentPos, splitted[j] + newline); else sci.InsertText(sci.CurrentPos, splitted[j]); sci.CurrentPos += sci.MBSafeTextLength(splitted[j]) + newline.Length; if (j > 0) { line = sci.LineFromPosition(sci.CurrentPos - newline.Length); newIndent = sci.GetLineIndentation(line) + indent; sci.SetLineIndentation(line, newIndent); } } Int32 length = sci.CurrentPos - currentPosition - newline.Length; Int32 delta = PostProcessSnippets(sci, currentPosition); return length + delta; } finally { sci.EndUndoAction(); } }