private bool TestIfComments(int pos, bool checkStr) { var st = sci.GetStyleAt(pos); //exclude autocomplete in comments and strings var b = st == TextStyle.MultilineStyle1 || st == TextStyle.MultilineStyle2 || st == TextStyle.Style6 || st == TextStyle.Style7; if (st == TextStyle.None && checkStr) { var lnn = sci.GetLineFromPosition(pos); var ln = sci.GetLine(lnn); var col = sci.GetColumnFromPosition(pos); for (var i = col; i > -1; i--) { if (sci.CharAt(sci.GetPositionByColumn(lnn, i)) == '"') { return(true); } } } return(b); }
public void RunSelected() { Func <String, CompiledAssembly> fun = s => app.GetService <ICodeBuilderService>(). RunBuilder <CompiledAssembly>(s, app.Document(), BuildOptions.Output | BuildOptions.TipError, ElaCodeBuilder.NoDebug, ElaCodeBuilder.NoWarnings); var sel = sci.HasSelections() ? sci.GetSelection().Text : sci.GetLine(sci.CurrentLine).Text; var src = sci.Text + "\r\n_=()\r\n" + sel.Trim(); var asm = fun(src); if (asm == null) { asm = fun(sel); } if (asm != null) { var svc = app.GetService <ICodeRunnerService>(); if (!svc.RunCode(asm, ExecOptions.PrintResult | ExecOptions.TipResult | ExecOptions.TipError | ExecOptions.LimitTime)) { asm = fun(sel); if (asm != null) { svc.RunCode(asm, ExecOptions.PrintResult | ExecOptions.TipResult | ExecOptions.TipError); } } } }
public void Print(string text) { if (InputDisabled) { return; } AppendText(text); sci.CaretPosition = sci.GetTextLength(); lastLen = sci.GetLine(sci.CurrentLine).Text.Trim('\0').Length; }
static private bool HandleDocTagCompletion(ScintillaControl Sci) { if (ASContext.CommonSettings.JavadocTags == null || ASContext.CommonSettings.JavadocTags.Length == 0) { return(false); } string txt = Sci.GetLine(Sci.LineFromPosition(Sci.CurrentPos)).TrimStart(); if (!Regex.IsMatch(txt, "^\\*[\\s]*\\@")) { return(false); } // build tag list if (docVariables == null) { docVariables = new List <ICompletionListItem>(); TagItem item; foreach (string tag in ASContext.CommonSettings.JavadocTags) { item = new TagItem(tag); docVariables.Add(item); } } // show CompletionList.Show(docVariables, true, ""); return(true); }
public override bool OnCompletionInsert(ScintillaControl sci, int position, string text, char trigger) { if (text == "Dictionary") { string insert = null; string line = sci.GetLine(sci.LineFromPosition(position)); Match m = Regex.Match(line, @"\svar\s+(?<varname>.+)\s*:\s*Dictionary\.<(?<indextype>.+)(?=(>\s*=))"); if (m.Success) { insert = String.Format(".<{0}>", m.Groups["indextype"].Value); } else { m = Regex.Match(line, @"\s*=\s*new"); if (m.Success) { ASResult result = ASComplete.GetExpressionType(sci, sci.PositionFromLine(sci.LineFromPosition(position)) + m.Index); if (result != null && !result.IsNull() && result.Member != null && result.Member.Type != null) { m = Regex.Match(result.Member.Type, @"(?<=<).+(?=>)"); if (m.Success) { insert = String.Format(".<{0}>", m.Value); } } } if (insert == null) { if (trigger == '.' || trigger == '(') { return(true); } insert = ".<>"; sci.InsertText(position + text.Length, insert); sci.CurrentPos = position + text.Length + 2; sci.SetSel(sci.CurrentPos, sci.CurrentPos); ASComplete.HandleAllClassesCompletion(sci, "", false, true); return(true); } } if (insert == null) { return(false); } if (trigger == '.') { sci.InsertText(position + text.Length, insert.Substring(1)); sci.CurrentPos = position + text.Length; } else { sci.InsertText(position + text.Length, insert); sci.CurrentPos = position + text.Length + insert.Length; } sci.SetSel(sci.CurrentPos, sci.CurrentPos); return(true); } return(base.OnCompletionInsert(sci, position, text, trigger)); }
static private void CompleteTemplate(string Context) { // get indentation ScintillaControl Sci = ASContext.CurSciControl; if (Sci == null) { return; } int position = Sci.CurrentPos; int line = Sci.LineFromPosition(position); int indent = Sci.LineIndentPosition(line) - Sci.PositionFromLine(line); string tab = Sci.GetLine(line).Substring(0, indent); // get EOL int eolMode = Sci.EOLMode; string newline = LineEndDetector.GetNewLineMarker(eolMode); CommentBlockStyle cbs = PluginBase.Settings.CommentBlockStyle; string star = cbs == CommentBlockStyle.Indented ? " *" : "*"; string parInd = cbs == CommentBlockStyle.Indented ? "\t" : " "; if (!PluginBase.MainForm.Settings.UseTabs) { parInd = " "; } // empty box if (Context == null) { Sci.ReplaceSel(newline + tab + star + " " + newline + tab + star + "/"); position += newline.Length + tab.Length + 1 + star.Length; Sci.SetSel(position, position); } // method details else { string box = newline + tab + star + " "; Match mFun = re_splitFunction.Match(Context); if (mFun.Success && !re_property.IsMatch(mFun.Groups["fname"].Value)) { // parameters MemberList list = ParseMethodParameters(mFun.Groups["params"].Value); foreach (MemberModel param in list) { box += newline + tab + star + " @param" + parInd + param.Name; } // return type Match mType = re_variableType.Match(mFun.Groups["type"].Value); if (mType.Success && !mType.Groups["type"].Value.Equals("void", StringComparison.OrdinalIgnoreCase)) { box += newline + tab + star + " @return"; //+mType.Groups["type"].Value; } } box += newline + tab + star + "/"; Sci.ReplaceSel(box); position += newline.Length + tab.Length + 1 + star.Length; Sci.SetSel(position, position); } }
private void FindFinished(FRResults results) { UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.UpdatingReferences")); MessageBar.Locked = true; bool isNotHaxe = !PluginBase.CurrentProject.Language.StartsWith("haxe"); bool packageIsNotEmpty = !string.IsNullOrEmpty(currentTarget.OldFileModel.Package); string targetName = Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); string oldType = (currentTarget.OldFileModel.Package + "." + targetName).Trim('.'); string newType = (currentTarget.NewPackage + "." + targetName).Trim('.'); foreach (KeyValuePair <string, List <SearchMatch> > entry in results) { List <SearchMatch> matches = entry.Value; if (matches.Count == 0) { continue; } string path = entry.Key; UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.Updating") + " \"" + path + "\""); ScintillaControl sci = AssociatedDocumentHelper.LoadDocument(path); if (isNotHaxe && path != currentTarget.NewFilePath && ASContext.Context.CurrentModel.Imports.Search(targetName, FlagType.Class & FlagType.Function & FlagType.Namespace, 0) == null) { ASGenerator.InsertImport(new MemberModel(targetName, newType, FlagType.Import, 0), false); } if (packageIsNotEmpty) { RefactoringHelper.ReplaceMatches(matches, sci, newType, null); } else { foreach (SearchMatch sm in matches) { if (sm.LineText.TrimStart().StartsWith("import")) { RefactoringHelper.SelectMatch(sci, sm); sci.ReplaceSel(newType); } } } foreach (SearchMatch match in matches) { match.LineText = sci.GetLine(match.Line - 1); match.Value = newType; } if (!Results.ContainsKey(path)) { Results[path] = new List <SearchMatch>(); } Results[path].AddRange(matches.ToArray()); PluginBase.MainForm.CurrentDocument.Save(); if (sci.IsModify) { AssociatedDocumentHelper.MarkDocumentToKeep(path); } } UserInterfaceManager.ProgressDialog.Hide(); MessageBar.Locked = false; UpdateReferencesNextTarget(); }
/// <summary> /// Convert multibyte column to byte length /// </summary> private int MBSafeColumn(ScintillaControl sci, int line, int length) { String text = sci.GetLine(line) ?? ""; length = Math.Min(length, text.Length); return(sci.MBSafeTextLength(text.Substring(0, length))); }
private void NewMatch(Match m) { match = m; position = m.Index; line = scintilla.LineFromPosition(position); column = scintilla.Column(position); text = m.Value; lineText = scintilla.GetLine(line); }
private bool IsVarDecl(ScintillaControl sci, int i) { if (features.Pattern == null) { return(false); } int line = sci.LineFromPosition(i); string text = sci.GetLine(line); return(features.Pattern.IsMatch(text)); }
private void AddBookmark(ScintillaControl sci, TreeNode parent, int line) { var txt = sci.GetLine(line).Text.TrimStart(' ', '\t'); txt = !String.IsNullOrEmpty(txt) && txt.Length > 30 ? txt.Substring(0, 30) + "..." : txt; var tn = new TreeNode(String.Format("Line {0}: {1}", (line + 1), txt)); tn.ImageKey = tn.SelectedImageKey = "Bookmark"; tn.Tag = line; parent.Nodes.Add(tn); tn.ContextMenuStrip = contextMenu; }
string ITextEditor.GetContent(Document doc, int lineNumber) { if (doc == App.Document()) { return(sci.GetLine(lineNumber).Text); } else { using (var sciTemp = new BasicScintillaControl()) { sciTemp.AttachDocument(((TextDocument)doc).GetSciDocument()); return(sciTemp.GetLine(lineNumber).Text); } } }
private String GetWordAtPosition(ScintillaControl sci, Int32 position) { if (position < 0) { return(null); } string characterClass = ScintillaControl.Configuration.GetLanguage(sci.ConfigurationLanguage).characterclass.Characters; int line = sci.LineFromPosition(position); int seek = sci.MBSafeCharPosition(position) - sci.MBSafeCharPosition(sci.PositionFromLine(line)); string text = sci.GetLine(line); if (seek < 0 || seek >= text.Length) { return(null); } if (!IsIdentifierChar(characterClass, text[seek]) && text[seek] != '.') { return(null); } // Search from the seek point to the left until we hit a non-alphanumeric which isn't a "." // "." must be handled specially so that expressions like player.health are easy to evaluate. int start = seek; while (start > 0 && (IsIdentifierChar(characterClass, text[start - 1]) || text[start - 1] == '.')) { --start; } // Search from the seek point to the right until we hit a non-alphanumeric int end = seek; while (end + 1 < text.Length && IsIdentifierChar(characterClass, text[end + 1])) { ++end; } return(text.Substring(start, end - start + 1)); }
/// <summary> /// Checks if bookmark list view needs updating /// </summary> private Boolean NeedRefresh(ScintillaControl sci, List <Int32> markers, ListView.ListViewItemCollection items) { if (items.Count != markers.Count) { return(true); } foreach (ListViewItem item in items) { Int32 marker = (Int32)item.Tag; if (!markers.Contains(marker)) { return(true); } if (sci.GetLine(marker).Trim() != item.SubItems[1].Text) { return(true); } } return(false); }
/// <summary> /// Update document bookmarks /// </summary> private void UpdateMarkers(String filename) { ITabbedDocument document = DocumentManager.FindDocument(filename); if (document == null || !document.IsEditable) { return; } ScintillaControl sci = document.SciControl; ListViewGroup group = this.FindGroup(document.FileName); if (group == null) { return; } List <Int32> markers = this.GetMarkers(document.SciControl); if (this.NeedRefresh(document.SciControl, markers, group.Items)) { Int32 index = 0; ListViewItem item; this.listView.BeginUpdate(); this.RemoveItemsFromGroup(group); ListViewItem[] items = new ListViewItem[markers.Count]; foreach (Int32 marker in markers) { item = new ListViewItem(new String[] { (marker + 1).ToString(), sci.GetLine(marker).Trim() }, 0); item.ToolTipText = sci.GetLine(marker).Trim(); item.Name = group.Name; item.Group = group; item.Tag = marker; items[index] = item; index++; } this.listView.Items.AddRange(items); group.Tag = markers; this.columnText.Width = -2; // Extend last column this.listView.EndUpdate(); } }
public static MemberModel GetTemplateBlockMember(ScintillaControl Sci, string blockTmpl) { if (string.IsNullOrEmpty(blockTmpl)) { return(null); } string firstLine = blockTmpl; int lineCount = 0; int index = blockTmpl.IndexOf("\n"); if (index != -1) { firstLine = blockTmpl.Substring(0, index); lineCount = Regex.Matches(blockTmpl, "\n").Count; } int lineNum = 0; while (lineNum < Sci.LineCount) { string line = Sci.GetLine(lineNum); int funcBlockIndex = line.IndexOf(firstLine); if (funcBlockIndex != -1) { MemberModel latest = new MemberModel(); latest.LineFrom = lineNum; latest.LineTo = lineNum; latest.LineTo = lineNum + lineCount; return(latest); } lineNum++; } return(null); }
private List <CssBlock> ParseBlocks(ScintillaControl sci) { List <CssBlock> blocks = new List <CssBlock>(); blocks.Clear(); int lines = sci.LineCount; int inString = 0; bool inComment = false; CssBlock block = null; for (int i = 0; i < lines; i++) { string line = sci.GetLine(i); int len = line.Length; int safeLen = len - 1; for (int j = 0; j < len; j++) { char c = line[j]; if (inComment) { if (c == '*' && j < safeLen && line[j + 1] == '/') { inComment = false; } else { continue; } } else if (inString > 0) { if (inString == 1 && c == '\'') { inString = 0; } else if (inString == 2 && c == '"') { inString = 0; } else { continue; } } else if (c == '\'') { inString = 1; } else if (c == '"') { inString = 2; } else if (c == '/' && j < safeLen && line[j + 1] == '/') { break; } else if (c == '/' && j < safeLen && line[j + 1] == '*') { inComment = true; } else if (c == '{') { CssBlock parent = block; block = new CssBlock(); block.LineFrom = i; block.ColFrom = j; if (parent != null) { block.Parent = parent; parent.Children.Add(block); } else { blocks.Add(block); } } else if (c == '}') { if (block != null) { block.LineTo = i; block.ColTo = j; block = block.Parent; if (block != null) { block.LineTo = i; block.ColTo = j; } } } } } return(blocks); }
/// <summary> /// /// </summary> private void UpdateReferencesNextTarget() { if (targets.Count > 0) { currentTarget = targets[0]; targets.Remove(currentTarget); FileModel oldFileModel = currentTarget.OldFileModel; FRSearch search; string newType; if (string.IsNullOrEmpty(oldFileModel.Package)) { search = new FRSearch("(package)+\\s*"); newType = Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } else { search = new FRSearch("(package)+\\s+(" + oldFileModel.Package + ")\\s*"); newType = oldFileModel.Package + "." + Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } search.IsRegex = true; search.Filter = SearchFilter.None; newType = newType.Trim('.'); MessageBar.Locked = true; string newFilePath = currentTarget.NewFilePath; ScintillaControl sci = AssociatedDocumentHelper.LoadDocument(newFilePath); List <SearchMatch> matches = search.Matches(sci.Text); RefactoringHelper.ReplaceMatches(matches, sci, "package " + currentTarget.NewPackage + " ", null); int offset = "package ".Length; foreach (SearchMatch match in matches) { match.Column += offset; match.LineText = sci.GetLine(match.Line - 1); match.Value = currentTarget.NewPackage; } if (!Results.ContainsKey(newFilePath)) { Results[newFilePath] = new List <SearchMatch>(); } Results[newFilePath].AddRange(matches.ToArray()); PluginBase.MainForm.CurrentDocument.Save(); if (sci.IsModify) { AssociatedDocumentHelper.MarkDocumentToKeep(currentTarget.OldFilePath); } MessageBar.Locked = false; UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.FindingReferences")); UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.SearchingFiles")); ASResult target = new ASResult() { Member = new MemberModel(newType, newType, FlagType.Import, 0) }; RefactoringHelper.FindTargetInFiles(target, UserInterfaceManager.ProgressDialog.UpdateProgress, FindFinished, true); } else { if (outputResults) { ReportResults(); } FireOnRefactorComplete(); } }
/// <summary> /// Handles the incoming character /// </summary> public static void OnChar(ScintillaControl sci, Int32 value) { if (cType == XMLType.Invalid || (sci.ConfigurationLanguage != "xml" && sci.ConfigurationLanguage != "html")) { return; } XMLContextTag ctag; Int32 position = sci.CurrentPos; if (sci.BaseStyleAt(position) == 6 && value != '"') { return; // in XML attribute } Char c = ' '; DataEvent de; switch (value) { case 10: // Shift+Enter to insert <BR/> Int32 line = sci.LineFromPosition(position); if (Control.ModifierKeys == Keys.Shift) { ctag = GetXMLContextTag(sci, position); if (ctag.Tag == null || ctag.Tag.EndsWith(">")) { int start = sci.PositionFromLine(line) - ((sci.EOLMode == 0)? 2:1); sci.SetSel(start, position); sci.ReplaceSel((PluginSettings.UpperCaseHtmlTags) ? "<BR/>" : "<br/>"); sci.SetSel(start + 5, start + 5); return; } } if (PluginSettings.SmartIndenter) { // Get last non-empty line String text = ""; Int32 line2 = line - 1; while (line2 >= 0 && text.Length == 0) { text = sci.GetLine(line2).TrimEnd(); line2--; } if ((text.EndsWith(">") && !text.EndsWith("?>") && !text.EndsWith("%>") && !closingTag.IsMatch(text)) || text.EndsWith("<!--") || text.EndsWith("<![CDATA[")) { // Get the previous tag do { position--; c = (Char)sci.CharAt(position); }while (position > 0 && c != '>'); ctag = GetXMLContextTag(sci, c == '>' ? position + 1 : position); if ((Char)sci.CharAt(position - 1) == '/') { return; } // Insert blank line if we pressed Enter between a tag & it's closing tag Int32 indent = sci.GetLineIndentation(line2 + 1); String checkStart = null; bool subIndent = true; if (text.EndsWith("<!--")) { checkStart = "-->"; subIndent = false; } else if (text.EndsWith("<![CDATA[")) { checkStart = "]]>"; subIndent = false; } else if (ctag.Closed) { subIndent = false; } else if (ctag.Name != null) { checkStart = "</" + ctag.Name; if (ctag.Name.ToLower() == "script" || ctag.Name.ToLower() == "style") { subIndent = false; } if (ctag.Tag.IndexOf('\r') > 0 || ctag.Tag.IndexOf('\n') > 0) { subIndent = false; } } if (checkStart != null) { text = sci.GetLine(line).TrimStart(); if (text.StartsWith(checkStart)) { sci.SetLineIndentation(line, indent); sci.InsertText(sci.PositionFromLine(line), LineEndDetector.GetNewLineMarker(sci.EOLMode)); } } // Indent the code if (subIndent) { indent += sci.Indent; } sci.SetLineIndentation(line, indent); position = sci.LineIndentPosition(line); sci.SetSel(position, position); return; } } break; case '<': case '/': if (value == '/') { if ((position < 2) || ((Char)sci.CharAt(position - 2) != '<')) { return; } ctag = new XMLContextTag(); ctag.Closing = true; } else { ctag = GetXMLContextTag(sci, position); if (ctag.Tag != null) { return; } } // Allow another plugin to handle this de = new DataEvent(EventType.Command, "XMLCompletion.Element", ctag); EventManager.DispatchEvent(PluginBase.MainForm, de); if (de.Handled) { return; } // New tag if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known) { List <ICompletionListItem> items = new List <ICompletionListItem>(); String previous = null; foreach (string ns in namespaces) { items.Add(new NamespaceItem(ns)); } foreach (HTMLTag tag in knownTags) { if (tag.Name != previous && (tag.NS == "" || tag.NS == defaultNS)) { items.Add(new HtmlTagItem(tag.Name, tag.Tag)); previous = tag.Name; } } items.Sort(new ListItemComparer()); CompletionList.Show(items, true); } return; case ':': ctag = GetXMLContextTag(sci, position); if (ctag.NameSpace == null || position - ctag.Position > ctag.Name.Length + 2) { return; } // Allow another plugin to handle this de = new DataEvent(EventType.Command, "XMLCompletion.Namespace", ctag); EventManager.DispatchEvent(PluginBase.MainForm, de); if (de.Handled) { return; } // Show namespace's tags if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known) { List <ICompletionListItem> items = new List <ICompletionListItem>(); String previous = null; foreach (HTMLTag tag in knownTags) { if (tag.Name != previous && tag.NS == ctag.NameSpace) { items.Add(new HtmlTagItem(tag.Name, tag.Name)); previous = tag.Name; } } CompletionList.Show(items, true); } return; case '>': if (PluginSettings.CloseTags) { ctag = GetXMLContextTag(sci, position); if (ctag.Name != null && !ctag.Closed) { // Allow another plugin to handle this de = new DataEvent(EventType.Command, "XMLCompletion.CloseElement", ctag); EventManager.DispatchEvent(PluginBase.MainForm, de); if (de.Handled) { return; } if (ctag.Closing) { return; } Boolean isLeaf = false; if (cType == XMLType.Known) { foreach (HTMLTag tag in knownTags) { if (String.Compare(tag.Tag, ctag.Name, true) == 0) { isLeaf = tag.IsLeaf; break; } } } if (isLeaf) { sci.SetSel(position - 1, position); sci.ReplaceSel("/>"); sci.SetSel(position + 1, position + 1); } else { String closeTag = "</" + ctag.Name + ">"; sci.ReplaceSel(closeTag); sci.SetSel(position, position); } } } return; case ' ': c = (char)sci.CharAt(position); if (c > 32 && c != '/' && c != '>' && c != '<') { return; } ctag = GetXMLContextTag(sci, position); if (ctag.Tag != null) { if (InQuotes(ctag.Tag) || ctag.Tag.LastIndexOf('"') < ctag.Tag.LastIndexOf('=')) { return; } // Allow another plugin to handle this Object[] obj = new Object[] { ctag, "" }; de = new DataEvent(EventType.Command, "XMLCompletion.Attribute", obj); EventManager.DispatchEvent(PluginBase.MainForm, de); if (de.Handled) { return; } if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known) { foreach (HTMLTag tag in knownTags) { if (String.Compare(tag.Tag, ctag.Name, true) == 0) { List <ICompletionListItem> items = new List <ICompletionListItem>(); String previous = null; foreach (String attr in tag.Attributes) { if (attr != previous) { items.Add(new HtmlAttributeItem(attr)); previous = attr; } } CompletionList.Show(items, true); return; } } } } /*else * { * if (Control.ModifierKeys == Keys.Shift) * { * sci.SetSel(position - 1, position); * sci.ReplaceSel(" "); * } * }*/ return; case '=': if (PluginSettings.InsertQuotes) { ctag = GetXMLContextTag(sci, position); position = sci.CurrentPos - 2; if (ctag.Tag != null && !String.IsNullOrEmpty(ctag.Name) && Char.IsLetter(ctag.Name[0]) && !InQuotes(ctag.Tag) && (GetWordLeft(sci, ref position).Length > 0)) { position = sci.CurrentPos; c = (Char)sci.CharAt(position); if (c > 32 && c != '>') { sci.ReplaceSel("\"\" "); } else { sci.ReplaceSel("\"\""); } sci.SetSel(position + 1, position + 1); justInsertedQuotesAt = position + 1; // Allow another plugin to handle this de = new DataEvent(EventType.Command, "XMLCompletion.AttributeValue", new Object[] { ctag, string.Empty }); EventManager.DispatchEvent(PluginBase.MainForm, de); } } return; case '"': ctag = GetXMLContextTag(sci, position); if (position > 1 && ctag.Tag != null && !ctag.Tag.StartsWith("<!")) { // TODO Colorize text change to highlight what's been done if (justInsertedQuotesAt == position - 1) { justInsertedQuotesAt = -1; c = (Char)sci.CharAt(position - 2); if (c == '"' && (Char)sci.CharAt(position - 2) == '"') { sci.SetSel(position - 2, position); sci.ReplaceSel("\""); } // Allow another plugin to handle this de = new DataEvent(EventType.Command, "XMLCompletion.AttributeValue", new Object[] { ctag, string.Empty }); EventManager.DispatchEvent(PluginBase.MainForm, de); } else { c = (Char)sci.CharAt(position - 1); if (c == '"' && (Char)sci.CharAt(position) == '"') { sci.SetSel(position - 1, position + 1); sci.ReplaceSel("\""); } } } break; case '?': case '%': if (PluginSettings.CloseTags && position > 1) { ctag = GetXMLContextTag(sci, position - 2); if (ctag.Tag == null || ctag.Tag.EndsWith(">")) { if ((Char)sci.CharAt(position - 2) == '<') { sci.ReplaceSel((Char)value + ">"); sci.SetSel(position, position); } } } break; case '!': if (PluginSettings.CloseTags && position > 1) { ctag = GetXMLContextTag(sci, position - 2); if (ctag.Tag == null || ctag.Tag.EndsWith(">")) { if ((Char)sci.CharAt(position - 2) == '<') { CompletionList.Show(xmlBlocks, true); } } } break; } }
private void UpdateReferencesNextTarget() { if (currentTargetIndex < targets.Count) { var currentTarget = targets[currentTargetIndex]; FileModel oldFileModel = currentTarget.OldFileModel; FRSearch search; string oldType; if (string.IsNullOrEmpty(oldFileModel.Package)) { search = new FRSearch("package"); search.WholeWord = true; oldType = Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } else { search = new FRSearch("package\\s+(" + oldFileModel.Package + ")"); oldType = oldFileModel.Package + "." + Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } search.IsRegex = true; search.Filter = SearchFilter.None; oldType = oldType.Trim('.'); MessageBar.Locked = true; string newFilePath = currentTarget.NewFilePath; var doc = AssociatedDocumentHelper.LoadDocument(currentTarget.TmpFilePath ?? newFilePath); ScintillaControl sci = doc.SciControl; search.SourceFile = sci.FileName; List <SearchMatch> matches = search.Matches(sci.Text); string packageReplacement = "package"; if (currentTarget.NewPackage != "") { packageReplacement += " " + currentTarget.NewPackage; } RefactoringHelper.ReplaceMatches(matches, sci, packageReplacement); int offset = "package ".Length; foreach (SearchMatch match in matches) { match.Column += offset; match.LineText = sci.GetLine(match.Line - 1); match.Value = currentTarget.NewPackage; } if (matches.Count > 0) { if (!Results.ContainsKey(newFilePath)) { Results[newFilePath] = new List <SearchMatch>(); } Results[newFilePath].AddRange(matches); } else if (sci.ConfigurationLanguage == "haxe") { // haxe modules don't need to specify a package if it's empty sci.InsertText(0, packageReplacement + ";\n\n"); } //Do we want to open modified files? //if (sci.IsModify) AssociatedDocumentHelper.MarkDocumentToKeep(file); doc.Save(); MessageBar.Locked = false; UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.FindingReferences")); UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.SearchingFiles")); currentTargetResult = RefactoringHelper.GetRefactorTargetFromFile(oldFileModel.FileName, AssociatedDocumentHelper); if (currentTargetResult != null) { RefactoringHelper.FindTargetInFiles(currentTargetResult, UserInterfaceManager.ProgressDialog.UpdateProgress, FindFinished, true, true, true); } else { currentTargetIndex++; UserInterfaceManager.ProgressDialog.Hide(); UpdateReferencesNextTarget(); } } else { MoveRefactoredFiles(); ReopenInitialFiles(); FireOnRefactorComplete(); } }
/// <summary> /// Handles the incoming character /// </summary> public static void OnChar(ScintillaControl sci, Int32 value) { if (cType == XMLType.Invalid || (sci.ConfigurationLanguage != "xml" && sci.ConfigurationLanguage != "html")) { return; } XMLContextTag ctag; Int32 position = sci.CurrentPos; if (sci.BaseStyleAt(position) == 6 && value != '"') { return; // in XML attribute } Char c = ' '; DataEvent de; switch (value) { case 10: // Shift+Enter to insert <BR/> Int32 line = sci.LineFromPosition(position); if (Control.ModifierKeys == Keys.Shift) { ctag = GetXMLContextTag(sci, position); if (ctag.Tag == null || ctag.Tag.EndsWith('>')) { int start = sci.PositionFromLine(line) - ((sci.EOLMode == 0)? 2:1); sci.SetSel(start, position); sci.ReplaceSel((PluginSettings.UpperCaseHtmlTags) ? "<BR/>" : "<br/>"); sci.SetSel(start + 5, start + 5); return; } } if (PluginSettings.SmartIndenter) { // There is no standard for XML formatting, although most IDEs have similarities. We are mostly going with Visual Studio style with slight differences. // Get last non-empty line. String text = ""; Int32 line2 = line - 1; while (line2 >= 0 && text.Length == 0) { text = sci.GetLine(line2).TrimEnd(); line2--; } if ((text.EndsWith('>') && !text.EndsWithOrdinal("?>") && !text.EndsWithOrdinal("%>")) || text.EndsWithOrdinal("<!--") || text.EndsWithOrdinal("<![CDATA[")) { // Get the previous tag. do { position--; c = (Char)sci.CharAt(position); }while (position > 0 && c != '>'); ctag = GetXMLContextTag(sci, c == '>' ? position + 1 : position); // Line indentation. Int32 indent = sci.GetLineIndentation(line2 + 1); String checkStart = null; bool subIndent = true; if (text.EndsWithOrdinal("<!--")) { checkStart = "-->"; subIndent = false; } else if (text.EndsWithOrdinal("<![CDATA[")) { checkStart = "]]>"; subIndent = false; } else if (ctag.Closed || ctag.Closing) { //Closed tag. Look for the nearest open and not closed tag for proper indentation subIndent = false; if (ctag.Name != null) { var tmpTags = new Stack <XMLContextTag>(); var tmpTag = ctag; if (!tmpTag.Closed) { tmpTags.Push(tmpTag); } while (tmpTag.Position != 0) { tmpTag = GetXMLContextTag(sci, tmpTag.Position); if (tmpTag.Tag != null && tmpTag.Name != null) { if (tmpTag.Closed) { continue; } else if (tmpTag.Closing) { tmpTags.Push(tmpTag); } else { if (tmpTags.Count > 0 && tmpTags.Peek().Name == tmpTag.Name) { tmpTags.Pop(); } else { break; } } } } if (tmpTags.Count > 0) { indent = sci.GetLineIndentation(sci.LineFromPosition(tmpTags.Pop().Position)); } else if (tmpTag.Name != null) { subIndent = true; checkStart = "</" + tmpTag.Name; indent = sci.GetLineIndentation(sci.LineFromPosition(tmpTag.Position)); } else { indent = sci.GetLineIndentation(sci.LineFromPosition(tmpTag.Position)); } } } else if (ctag.Name != null) { // Indentation. Some IDEs use the tag position, VS uses the tag start line indentation. indent = sci.GetLineIndentation(sci.LineFromPosition(ctag.Position)); checkStart = "</" + ctag.Name; if (ctag.Name.ToLower() == "script" || ctag.Name.ToLower() == "style") { subIndent = false; } } try { sci.BeginUndoAction(); if (checkStart != null) { text = sci.GetLine(line).TrimStart(); if (text.StartsWithOrdinal(checkStart)) { sci.SetLineIndentation(line, indent); sci.InsertText(sci.PositionFromLine(line), LineEndDetector.GetNewLineMarker(sci.EOLMode)); } } // Indent the code if (subIndent) { indent += sci.Indent; } sci.SetLineIndentation(line, indent); position = sci.LineIndentPosition(line); sci.SetSel(position, position); } finally { sci.EndUndoAction(); } return; } else if (!text.EndsWith('>')) { ctag = GetXMLContextTag(sci, sci.CurrentPos); if (ctag.Tag == null || ctag.Name == null) { return; } // We're inside a tag. Visual Studio indents with regards to the first line, other IDEs indent using the indentation of the last line with text. int indent; string tag = (ctag.Tag.IndexOf('\r') > 0 || ctag.Tag.IndexOf('\n') > 0) ? ctag.Tag.Substring(0, ctag.Tag.IndexOfAny(new[] { '\r', '\n' })).TrimEnd() : ctag.Tag.TrimEnd(); if (tag.EndsWith('\"')) { int i; int l = tag.Length; for (i = ctag.Name.Length + 1; i < l; i++) { if (!char.IsWhiteSpace(tag[i])) { break; } } indent = sci.Column(ctag.Position) + sci.MBSafePosition(i); } else { indent = sci.GetLineIndentation(sci.LineFromPosition(ctag.Position)) + sci.Indent; } sci.SetLineIndentation(line, indent); position = sci.LineIndentPosition(line); sci.SetSel(position, position); return; } } break; case '<': case '/': if (value == '/') { if ((position < 2) || ((Char)sci.CharAt(position - 2) != '<')) { return; } ctag = new XMLContextTag(); ctag.Position = position - 2; ctag.Closing = true; } else { ctag = GetXMLContextTag(sci, position); if (ctag.Tag != null) { return; } } // Allow another plugin to handle this de = new DataEvent(EventType.Command, "XMLCompletion.Element", ctag); EventManager.DispatchEvent(PluginBase.MainForm, de); if (de.Handled) { return; } // New tag if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known) { List <ICompletionListItem> items = new List <ICompletionListItem>(); String previous = null; foreach (string ns in namespaces) { items.Add(new NamespaceItem(ns)); } foreach (HTMLTag tag in knownTags) { if (tag.Name != previous && (tag.NS == "" || tag.NS == defaultNS)) { items.Add(new HtmlTagItem(tag.Name, tag.Tag)); previous = tag.Name; } } items.Sort(new ListItemComparer()); CompletionList.Show(items, true); } return; case ':': ctag = GetXMLContextTag(sci, position); if (ctag.NameSpace == null || position - ctag.Position > ctag.Name.Length + 2) { return; } // Allow another plugin to handle this de = new DataEvent(EventType.Command, "XMLCompletion.Namespace", ctag); EventManager.DispatchEvent(PluginBase.MainForm, de); if (de.Handled) { return; } // Show namespace's tags if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known) { List <ICompletionListItem> items = new List <ICompletionListItem>(); String previous = null; foreach (HTMLTag tag in knownTags) { if (tag.Name != previous && tag.NS == ctag.NameSpace) { items.Add(new HtmlTagItem(tag.Name, tag.Name)); previous = tag.Name; } } CompletionList.Show(items, true); } return; case '>': if (PluginSettings.CloseTags) { ctag = GetXMLContextTag(sci, position); if (ctag.Name != null && !ctag.Closed) { // Allow another plugin to handle this de = new DataEvent(EventType.Command, "XMLCompletion.CloseElement", ctag); EventManager.DispatchEvent(PluginBase.MainForm, de); if (de.Handled) { return; } if (ctag.Closing) { return; } Boolean isLeaf = false; if (cType == XMLType.Known) { foreach (HTMLTag tag in knownTags) { if (String.Compare(tag.Tag, ctag.Name, true) == 0) { isLeaf = tag.IsLeaf; break; } } } if (isLeaf) { sci.SetSel(position - 1, position); sci.ReplaceSel("/>"); sci.SetSel(position + 1, position + 1); } else { String closeTag = "</" + ctag.Name + ">"; sci.ReplaceSel(closeTag); sci.SetSel(position, position); } } } return; case ' ': c = (char)sci.CharAt(position); if (c > 32 && c != '/' && c != '>' && c != '<') { return; } ctag = GetXMLContextTag(sci, position); if (ctag.Tag != null) { if (InQuotes(ctag.Tag) || ctag.Tag.LastIndexOf('"') < ctag.Tag.LastIndexOf('=')) { return; } // Allow another plugin to handle this Object[] obj = new Object[] { ctag, "" }; de = new DataEvent(EventType.Command, "XMLCompletion.Attribute", obj); EventManager.DispatchEvent(PluginBase.MainForm, de); if (de.Handled) { return; } if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known) { foreach (HTMLTag tag in knownTags) { if (String.Compare(tag.Tag, ctag.Name, true) == 0) { List <ICompletionListItem> items = new List <ICompletionListItem>(); String previous = null; foreach (String attr in tag.Attributes) { if (attr != previous) { items.Add(new HtmlAttributeItem(attr)); previous = attr; } } CompletionList.Show(items, true); return; } } } } /*else * { * if (Control.ModifierKeys == Keys.Shift) * { * sci.SetSel(position - 1, position); * sci.ReplaceSel(" "); * } * }*/ return; case '=': if (PluginSettings.InsertQuotes) { ctag = GetXMLContextTag(sci, position); position = sci.CurrentPos - 2; if (ctag.Tag != null && !String.IsNullOrEmpty(ctag.Name) && Char.IsLetter(ctag.Name[0]) && !InQuotes(ctag.Tag) && (GetWordLeft(sci, ref position).Length > 0)) { position = sci.CurrentPos; c = (Char)sci.CharAt(position); if (c > 32 && c != '>') { sci.ReplaceSel("\"\" "); } else { sci.ReplaceSel("\"\""); } sci.SetSel(position + 1, position + 1); justInsertedQuotesAt = position + 1; // Allow another plugin to handle this de = new DataEvent(EventType.Command, "XMLCompletion.AttributeValue", new Object[] { ctag, string.Empty }); EventManager.DispatchEvent(PluginBase.MainForm, de); } } return; case '"': ctag = GetXMLContextTag(sci, position); if (position > 1 && ctag.Tag != null && !ctag.Tag.StartsWithOrdinal("<!")) { // TODO Colorize text change to highlight what's been done if (justInsertedQuotesAt == position - 1) { justInsertedQuotesAt = -1; c = (Char)sci.CharAt(position - 2); if (c == '"' && (Char)sci.CharAt(position - 2) == '"') { sci.SetSel(position - 2, position); sci.ReplaceSel("\""); } // Allow another plugin to handle this de = new DataEvent(EventType.Command, "XMLCompletion.AttributeValue", new Object[] { ctag, string.Empty }); EventManager.DispatchEvent(PluginBase.MainForm, de); } else { c = (Char)sci.CharAt(position - 1); if (c == '"' && (Char)sci.CharAt(position) == '"') { sci.SetSel(position - 1, position + 1); sci.ReplaceSel("\""); } } } break; case '?': case '%': if (PluginSettings.CloseTags && position > 1) { ctag = GetXMLContextTag(sci, position - 2); if (ctag.Tag == null || ctag.Tag.EndsWith('>')) { if ((Char)sci.CharAt(position - 2) == '<') { sci.ReplaceSel((Char)value + ">"); sci.SetSel(position, position); } } } break; case '!': if (PluginSettings.CloseTags && position > 1) { ctag = GetXMLContextTag(sci, position - 2); if (ctag.Tag == null || ctag.Tag.EndsWith('>')) { if ((Char)sci.CharAt(position - 2) == '<') { CompletionList.Show(xmlBlocks, true); } } } break; } }
/// <summary> /// Add closing brace to a code block. /// If enabled, move the starting brace to a new line. /// </summary> /// <param name="Sci"></param> /// <param name="txt"></param> /// <param name="line"></param> public static void AutoCloseBrace(ScintillaControl Sci, int line) { // find matching brace int bracePos = Sci.LineEndPosition(line - 1) - 1; while ((bracePos > 0) && (Sci.CharAt(bracePos) != '{')) { bracePos--; } if (bracePos == 0 || Sci.BaseStyleAt(bracePos) != 5) { return; } int match = Sci.SafeBraceMatch(bracePos); int start = line; int indent = Sci.GetLineIndentation(start - 1); if (match > 0) { int endIndent = Sci.GetLineIndentation(Sci.LineFromPosition(match)); if (endIndent + Sci.TabWidth > indent) { return; } } // find where to include the closing brace int startIndent = indent; int count = Sci.LineCount; int lastLine = line; int position; string txt = Sci.GetLine(line).Trim(); line++; int eolMode = Sci.EOLMode; string NL = LineEndDetector.GetNewLineMarker(eolMode); if (txt.Length > 0 && ")]};,".IndexOf(txt[0]) >= 0) { Sci.BeginUndoAction(); try { position = Sci.CurrentPos; Sci.InsertText(position, NL + "}"); Sci.SetLineIndentation(line, startIndent); } finally { Sci.EndUndoAction(); } return; } else { while (line < count - 1) { txt = Sci.GetLine(line).TrimEnd(); if (txt.Length != 0) { indent = Sci.GetLineIndentation(line); if (indent <= startIndent) { break; } lastLine = line; } else { break; } line++; } } if (line >= count - 1) { lastLine = start; } // insert closing brace Sci.BeginUndoAction(); try { position = Sci.LineEndPosition(lastLine); Sci.InsertText(position, NL + "}"); Sci.SetLineIndentation(lastLine + 1, startIndent); } finally { Sci.EndUndoAction(); } }
internal void OnCharAdded(ScintillaControl sci, int position, int value) { if (!enabled) { return; } bool autoInsert = false; char c = (char)value; if (wordChars.IndexOf(c) < 0) { if (c == ':') { if (lastColonInsert == position - 1) { sci.DeleteBack(); lastColonInsert = -1; return; } } else if (c == ';') { char c2 = (char)sci.CharAt(position); if (c2 == ';') { sci.DeleteBack(); sci.SetSel(position, position); return; } } else if (c == '\n' && !settings.DisableAutoCloseBraces) { int line = sci.LineFromPosition(position); string text = sci.GetLine(line - 1).TrimEnd(); if (text.EndsWith('{')) { AutoCloseBrace(sci, line); } } else if (c == '\t') // TODO get tab notification! { position--; autoInsert = true; } else { return; } } var context = GetContext(sci, position); var mode = CompleteMode.None; if (context.InComments) { return; } else if (context.InBlock) { if (context.Word == "-") { mode = CompleteMode.Prefix; } else if (context.Word.Length >= 2 || (char)value == '-') { mode = CompleteMode.Attribute; } } else if (context.InValue) { if (features.Mode != "CSS" && c == features.Trigger) { context.Word = context.Word.Substring(1); context.Position++; mode = CompleteMode.Variable; } else if (context.Word.Length == 1 && "abcdefghijklmnopqrstuvwxyz".IndexOf(context.Word[0]) >= 0) { mode = CompleteMode.Value; } } else if (c == ':' && !context.IsVar) { mode = CompleteMode.Pseudo; } HandleCompletion(mode, context, autoInsert, true); }
static public void OnChar(ScintillaControl sci, int value) { if (cType == XMLType.Invalid) { return; } XMLContextTag ctag; int position = sci.CurrentPos; char c = ' '; switch (value) { case 10: int line = sci.LineFromPosition(position); // Shift+Enter to insert <BR/> if (Control.ModifierKeys == Keys.Shift) { ctag = GetXMLContextTag(sci, position); if (ctag.Tag == null || ctag.Tag.EndsWith(">")) { int start = sci.PositionFromLine(line) - ((sci.EOLMode == 0)? 2:1); sci.SetSel(start, position); sci.ReplaceSel((lowerCaseHtmlTags)?"<br/>":"<BR/>"); sci.SetSel(start + 5, start + 5); return; } } if (autoIndent) { // get last non-empty line string text = ""; int line2 = line - 1; while (line2 >= 0 && text.Length == 0) { text = sci.GetLine(line2).TrimEnd(); line2--; } if ((text.EndsWith(">") && !text.EndsWith("?>") && !text.EndsWith("%>") && !re_closingTag.IsMatch(text)) || text.EndsWith("<!--") || text.EndsWith("<![CDATA[")) { // get the previous tag do { position--; c = (char)sci.CharAt(position); }while (position > 0 && c != '>'); ctag = GetXMLContextTag(sci, position); if ((char)sci.CharAt(position - 1) == '/') { return; } // insert blank line if we pressed Enter between a tag & it's closing tag int indent = sci.GetLineIndentation(line2 + 1); string checkStart = null; if (text.EndsWith("<!--")) { checkStart = "-->"; } else if (text.EndsWith("<![CDATA[")) { checkStart = "]]>"; } else if (ctag.Name != null) { checkStart = "</" + ctag.Name; } if (checkStart != null) { text = sci.GetLine(line).TrimStart(); if (text.StartsWith(checkStart)) { sci.SetLineIndentation(line, indent); sci.InsertText(sci.PositionFromLine(line), mainForm.GetNewLineMarker(sci.EOLMode)); } } // indent sci.SetLineIndentation(line, indent + sci.Indent); position = sci.LineIndentPosition(line); sci.SetSel(position, position); return; } } break; case '<': case '/': if (value == '/') { if ((position < 2) || ((char)sci.CharAt(position - 2) != '<')) { return; } } else { ctag = GetXMLContextTag(sci, position); if (ctag.Tag != null) { return; } } // new tag if (enableHtmlCompletion && cType == XMLType.Known) { ArrayList items = new ArrayList(); string previous = null; foreach (HTMLTag tag in knownTags) { if (tag.Name != previous) { items.Add(new HtmlTagItem(tag.Name, tag.Tag)); previous = tag.Name; } } CompletionList.Show(items, true); } else { // allow another plugin to handle this mainForm.DispatchEvent(new DataEvent(EventType.CustomData, "XMLCompletion.Element", new XMLContextTag())); } return; case '>': if (autoCloseTags) { ctag = GetXMLContextTag(sci, position); if (ctag.Name != null && !ctag.Tag.EndsWith("/>")) { // est-ce un tag sans enfant? bool isLeaf = false; if (cType == XMLType.Known) { foreach (HTMLTag tag in knownTags) { if (String.Compare(tag.Tag, ctag.Name, true) == 0) { isLeaf = tag.IsLeaf; break; } } } if (isLeaf) { sci.SetSel(position - 1, position); sci.ReplaceSel("/>"); sci.SetSel(position + 1, position + 1); } else { string closeTag = "</" + ctag.Name + ">"; sci.ReplaceSel(closeTag); sci.SetSel(position, position); } } } return; case ' ': c = (char)sci.CharAt(position); if (c > 32 && c != '/' && c != '>' && c != '<') { return; } ctag = GetXMLContextTag(sci, position); if (ctag.Tag != null) { if (InQuotes(ctag.Tag) || ctag.Tag.LastIndexOf('"') < ctag.Tag.LastIndexOf('=')) { return; } // if (enableHtmlCompletion && cType == XMLType.Known) { foreach (HTMLTag tag in knownTags) { if (String.Compare(tag.Tag, ctag.Name, true) == 0) { ArrayList items = new ArrayList(); string previous = null; foreach (string attr in tag.Attributes) { if (attr != previous) { items.Add(new HtmlAttributeItem(attr)); previous = attr; } } CompletionList.Show(items, true); return; } } } else { // allow another plugin to handle this object[] o = new object[] { ctag, "" }; mainForm.DispatchEvent(new DataEvent(EventType.CustomData, "XMLCompletion.Attribute", o)); } } return; case '=': if (insertQuotes) { ctag = GetXMLContextTag(sci, position); position = sci.CurrentPos - 2; if (ctag.Tag != null && !InQuotes(ctag.Tag) && (GetWordLeft(sci, ref position).Length > 0)) { position = sci.CurrentPos; c = (char)sci.CharAt(position); if (c > 32 && c != '>') { sci.ReplaceSel("\"\" "); } else { sci.ReplaceSel("\"\""); } sci.SetSel(position + 1, position + 1); } } return; case '?': case '%': if (autoCloseTags && position > 1) { ctag = GetXMLContextTag(sci, position - 2); if (ctag.Tag == null || ctag.Tag.EndsWith(">")) { if ((char)sci.CharAt(position - 2) == '<') { sci.ReplaceSel((char)value + ">"); sci.SetSel(position, position); } } } break; case '!': if (autoCloseTags && position > 1) { ctag = GetXMLContextTag(sci, position - 2); if (ctag.Tag == null || ctag.Tag.EndsWith(">")) { if ((char)sci.CharAt(position - 2) == '<') { CompletionList.Show(xmlBlocks, true); } } } break; } }