public static string GetSuggestionHint() { var document = Npp.GetCurrentDocument(); int currentPos = document.GetCurrentPos(); string word = document.GetWordAtCursor(); if (word != "") { return(word); } // string text = document.GetTextBetween(Math.Max(0, currentPos - 30), currentPos); //check up to 30 chars from left // int pos = text.LastIndexOfAny(SimpleCodeCompletion.Delimiters); // if (pos != -1) // { // string token = text.Substring(pos + 1);// +justTypedText; // return token.Trim(); // } return(null); }
static public void FinalizeCurrent() { var indicators = Npp.FindIndicatorRanges(SnippetContext.indicatorId); foreach (var range in indicators) { Npp.ClearIndicator(SnippetContext.indicatorId, range.X, range.Y); } var caretPoint = indicators.Where(point => { string text = Npp.GetTextBetween(point); return(text == " " || text == "|"); }) .FirstOrDefault(); if (caretPoint.X != caretPoint.Y) { Npp.SetTextBetween("", caretPoint); Npp.SetSelection(caretPoint.X, caretPoint.X); } }
private void AutocompleteForm_Load(object sender, EventArgs e) { try { var g = listBox1.CreateGraphics(); itemHeight = (int)g.MeasureString("T", listBox1.Font).Height; listBox1.Sorted = false; listBox1.DrawMode = DrawMode.OwnerDrawVariable; listBox1.DrawItem += listBox1_DrawItem; listBox1.MeasureItem += listBox1_MeasureItem; listBox1.HorizontalScrollbar = true; FilterFor(initialPartialName); // ListenToKeyStroks(true); Capture = true; MouseDown += AutocompleteForm_MouseDown; timer1.Enabled = true; if (Config.Instance.AutoInsertSingeSuggestion && listBox1.Items.Count == 1) { var document = Npp.GetCurrentDocument(); var caret = document.GetCurrentPos(); string lineLeftPart = document.GetTextBetween(Math.Max(0, caret - 200), caret).GetLines().LastOrDefault(); string textOnLeft = lineLeftPart.TrimEnd(); if (!textOnLeft.EndsWith("=")) { // bad UX if it is a '=' character. For example typing 'var i =' will automatically insert 'new int'. listBox1.SelectedIndex = 0; OnAutocompletionAccepted(listBox1.SelectedItem as ICompletionData); } } } catch { } //FilterFor may close the form }
public static void FormatDocument() { try { var document = Npp.GetCurrentDocument(); int currentPos = document.GetCurrentPos(); CaretBeforeLastFormatting = currentPos; string code = document.GetTextBetween(0, npp.DocEnd); if (code.Any() && currentPos != -1 && currentPos < code.Length) { code = NormalizeNewLines(code, ref currentPos); int topScrollOffset = document.LineFromPosition(currentPos) - document.GetFirstVisibleLine(); TopScrollOffsetBeforeLastFormatting = topScrollOffset; string newCode = FormatCode(code, ref currentPos, Npp.Editor.GetCurrentFilePath()); if (newCode != null) { document.SetText(newCode); document.SetCurrentPos(currentPos); document.ClearSelection(); document.SetFirstVisibleLine(document.LineFromPosition(currentPos) - topScrollOffset); } } } catch { #if DEBUG throw; #endif //formatting errors are not critical so can be ignored in release mode } }
public static string ProcessKeyPress(char keyChar) { var document = Npp.GetCurrentDocument(); var currentPos = document.GetCurrentPos().Value; string justTypedText = ""; if (keyChar == 8) { document.SetSelection(currentPos - 1, currentPos); document.ReplaceSel(""); } else { if (keyChar != 0) { justTypedText = keyChar.ToString(); document.ReplaceSelection(justTypedText); } } return(justTypedText); }
static IEnumerable <ICompletionData> GetCSharpScriptCompletionData(string editorText, int offset) { var directiveLine = GetCSharpScriptDirectiveLine(editorText, offset); if (directiveLine.StartsWith("//css_")) //e.g. '//css_ref' { var word = Npp.GetWordAtPosition(offset); //e.g. 'css_ref' if (word.StartsWith("css_")) //directive itself { return(CssCompletionData.AllDirectives); } else //directive is complete and user is typing the next word (directive argument) { if (directiveLine.StartsWith("//css_ref")) { return(CssCompletionData.DefaultRefAsms); } } } return(null); }
public static string ProcessKeyPress(char keyChar) { int currentPos = Npp.GetCaretPosition(); IntPtr sci = Npp.CurrentScintilla; string justTypedText = ""; if (keyChar == 8) { Win32.SendMessage(sci, SciMsg.SCI_SETSELECTIONSTART, currentPos - 1, 0); Win32.SendMessage(sci, SciMsg.SCI_SETSELECTIONEND, currentPos, 0); Win32.SendMessage(sci, SciMsg.SCI_REPLACESEL, 0, ""); } else { if (keyChar != 0) { justTypedText = keyChar.ToString(); Win32.SendMessage(sci, SciMsg.SCI_REPLACESEL, justTypedText); } } return(justTypedText); }
public void CheckIfNeedsClosing() { if (IsShowing && !simple && lastMethodStartPos.HasValue) { int methodStartPos = lastMethodStartPos.Value; string text; popupForm.ProcessMethodOverloadHint(NppEditor.GetMethodOverloadHint(methodStartPos, out text)); int currentPos = Npp.GetCurrentDocument().GetCurrentPos(); if (currentPos <= methodStartPos) //user removed/substituted method token as the result of keyboard input { base.Close(); } else if (text != null && text[text.Length - 1] == ')') { string typedArgs = text; if (NRefactoryExtensions.AreBracketsClosed(typedArgs)) { base.Close(); } } } }
static public bool NavigateToNextParam(SnippetContext context) { var document = Npp.GetCurrentDocument(); var indicators = document.FindIndicatorRanges(SnippetContext.indicatorId); if (!indicators.Any()) { return(false); } Point currentParam = context.CurrentParameter.Value; string currentParamOriginalText = context.CurrentParameterValue; document.SetSelection(currentParam.X, currentParam.X); string currentParamDetectedText = document.GetWordAtCursor("\t\n\r ,;'\"".ToCharArray()); if (currentParamOriginalText != currentParamDetectedText) { //current parameter is modified, indicator is destroyed so restore the indicator first document.SetIndicatorStyle(SnippetContext.indicatorId, SciMsg.INDIC_BOX, Color.Blue); document.PlaceIndicator(SnippetContext.indicatorId, currentParam.X, currentParam.X + currentParamDetectedText.Length); indicators = document.FindIndicatorRanges(SnippetContext.indicatorId);//needs refreshing as the document is modified var paramsInfo = indicators.Select(p => new { Index = indicators.IndexOf(p), Text = document.GetTextBetween(p), Range = p, Pos = p.X }) .OrderBy(x => x.Pos) .ToArray(); var paramsToUpdate = paramsInfo.Where(item => item.Text == currentParamOriginalText).ToArray(); foreach (var param in paramsToUpdate) { Snippets.ReplaceTextAtIndicator(currentParamDetectedText, indicators[param.Index]); indicators = document.FindIndicatorRanges(SnippetContext.indicatorId);//needs refreshing as the document is modified } } Point?nextParameter = null; int currentParamIndex = indicators.FindIndex(x => x.X >= currentParam.X); //can also be logical 'next' var prevParamsValues = indicators.Take(currentParamIndex).Select(p => document.GetTextBetween(p)).ToList(); prevParamsValues.Add(currentParamOriginalText); prevParamsValues.Add(currentParamDetectedText); prevParamsValues.Add(" "); prevParamsValues.Add("|"); foreach (var range in indicators.ToArray()) { if (currentParam.X < range.X && !prevParamsValues.Contains(document.GetTextBetween(range))) { nextParameter = range; break; } } if (!nextParameter.HasValue) { nextParameter = indicators.FirstOrDefault(); } context.CurrentParameter = nextParameter; if (context.CurrentParameter.HasValue) { document.SetSelection(context.CurrentParameter.Value.X, context.CurrentParameter.Value.Y); context.CurrentParameterValue = document.GetTextBetween(context.CurrentParameter.Value); } return(true); }
public static IEnumerable <ICompletionData> GetCompletionData(string editorText, int offset, string fileName, bool isControlSpace = true) // not the best way to put in the whole string every time { try { if (string.IsNullOrEmpty(editorText)) { return(new ICompletionData[0]); } var includeSpec = $"//css_inc {Config.Instance.DefaultIncludeFile}" + Environment.NewLine; var effectiveCode = includeSpec + editorText; var effectiveOffset = offset + includeSpec.Length; var data = Syntaxer.GetCompletions(effectiveCode, fileName, effectiveOffset).ToList(); //suggest default CS-Script usings as well var extraItems = new List <ICompletionData>(); var document = Npp.GetCurrentDocument(); var line = document.GetLine(document.LineFromPosition(offset)).Trim(); bool isUsing = (line == "using"); if (isUsing) { extraItems.AddRange(CssCompletionData.DefaultNamespaces); extraItems.ForEach(x => x.CompletionText = x.CompletionText + ";"); } int length = Math.Min(editorText.Length - offset, 20); string rightHalfOfLine = editorText.Substring(offset, length) .Split(new[] { '\n' }, 2).FirstOrDefault(); data.ForEach(x => { if (isUsing) { x.CompletionText = x.CompletionText + ";"; } else if (Config.Instance.UseMethodBrackets) { if (x.CompletionType == CompletionType.method || x.CompletionType == CompletionType.extension_method) { //"Console.WriteLi| " but not "Console.Write|(" if (rightHalfOfLine == null || rightHalfOfLine.StartsWith(" ") || rightHalfOfLine.StartsWith("\r") || rightHalfOfLine.StartsWith("\n")) { x.CompletionText += "("; if (x.InvokeParametersSet) { if (x.InvokeParameters.Count() == 0 || (x.InvokeParameters.Count() == 1 && x.CompletionType == CompletionType.extension_method)) { x.CompletionText += ")"; //like .Clone() } } } } } }); return(data.Concat(extraItems)); } catch (Exception e) { e.LogAsDebug(); return(new ICompletionData[0]); //the exception can happens even for the internal NRefactor-related reasons } }
static public string GetWordAtCursor(this ScintillaGateway document, out Point point, char[] wordDelimiters = null) { int currentPos = document.GetCurrentPos(); return(Npp.GetCurrentDocument().GetWordAtPosition(currentPos, out point, wordDelimiters)); }
static public int GetPositionFromLineColumn(int line, int column) { return(Npp.GetLineStart(line) + column); }
static public string GetShortcutsFile() { return(Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Npp.GetConfigDir())), "shortcuts.xml")); }
static public void EditSnippetsConfig() { Npp.OpenFile(Snippets.ConfigFile); }
public Shortcuts() { var configDir = Path.Combine(Npp.GetConfigDir(), "CSharpIntellisense"); base.file = Path.Combine(configDir, "shortcuts.ini"); }
public void TriggerPopup(bool simple, int methodStartPos, string[] data) { try { this.simple = simple; lastMethodStartPos = methodStartPos; base.Popup( form => //on opening { if (!simple) { form.LeftBottomCorner = Npp.GetCurrentDocument().GetCaretScreenLocation(); } else { form.LeftBottomCorner = Cursor.Position; } form.Simple = simple; form.AddData(data); KeyInterceptor.Instance.Add(Keys.Escape); KeyInterceptor.Instance.KeyDown += Instance_KeyDown; if (!simple) { KeyInterceptor.Instance.Add(Keys.Down, Keys.Up, Keys.Escape, Keys.Enter, Keys.Delete, Keys.Back); form.KeyPress += (sender, e) => { NppEditor.ProcessKeyPress(e.KeyChar); Plugin.OnCharTyped(e.KeyChar); CheckIfNeedsClosing(); }; form.KeyDown += (sender, e) => { if (e.KeyCode == Keys.Delete) { NppEditor.ProcessDeleteKeyDown(); } }; form.ProcessMethodOverloadHint(NppEditor.GetMethodOverloadHint(methodStartPos)); Task.Factory.StartNew(() => { Rectangle rect = npp.GetClientRect(); while (popupForm != null) { try { Npp.GetCurrentDocument().GrabFocus(); var newRect = npp.GetClientRect(); if (rect != newRect) //if NPP moved, resized close the popup { base.Close(); return; } Thread.Sleep(500); } catch { base.Close(); return; } } }); } }, form => //on closing { KeyInterceptor.Instance.KeyDown -= Instance_KeyDown; }); } catch { } }
public static IEnumerable <ICompletionData> GetCompletionData(string editorText, int offset, string fileName, bool isControlSpace = true) // not the best way to put in the whole string every time { try { if (string.IsNullOrEmpty(editorText)) { return(new ICompletionData[0]); } var effectiveOffset = offset; // VS experience // if (offset > 0) // { // for (int i = offset - 1; i >= 0; i--) // { // if (char.IsWhiteSpace(editorText[i])) // { // break; // } // else if (editorText[i] == '.') // { // effectiveOffset = i + 1; // } // } // } var data = Config.Instance.UsingRoslyn ? Syntaxer.GetCompletions(editorText, fileName, effectiveOffset).ToList() : MonoEngine.GetCompletionData(editorText, effectiveOffset, fileName, isControlSpace).ToList(); // var data = (GetCSharpScriptCompletionData(editorText, offset) ?? // (Config.Instance.UsingRoslyn ? // RoslynEngine.GetCompletionData(editorText, offset, fileName, isControlSpace) : // MonoEngine.GetCompletionData(editorText, offset, fileName, isControlSpace)) // ).ToList(); //suggest default CS-Script usings as well var extraItems = new List <ICompletionData>(); var document = Npp.GetCurrentDocument(); var line = document.GetLine(document.LineFromPosition(offset)).Trim(); bool isUsing = (line == "using"); if (isUsing) { extraItems.AddRange(CssCompletionData.DefaultNamespaces); extraItems.ForEach(x => x.CompletionText = x.CompletionText + ";"); } int length = Math.Min(editorText.Length - offset, 20); string rightHalfOfLine = editorText.Substring(offset, length) .Split(new[] { '\n' }, 2).FirstOrDefault(); data.ForEach(x => { if (isUsing) { x.CompletionText = x.CompletionText + ";"; } else if (Config.Instance.UseMethodBrackets) { if (x.CompletionType == CompletionType.method || x.CompletionType == CompletionType.extension_method) { //"Console.WriteLi| " but not "Console.Write|(" if (rightHalfOfLine == null || rightHalfOfLine.StartsWith(" ") || rightHalfOfLine.StartsWith("\r") || rightHalfOfLine.StartsWith("\n")) { x.CompletionText += "("; if (x.InvokeParametersSet) { if (x.InvokeParameters.Count() == 0 || (x.InvokeParameters.Count() == 1 && x.CompletionType == CompletionType.extension_method)) { x.CompletionText += ")"; //like .Clone() } } } } } }); return(data.Concat(extraItems)); } catch (Exception e) { e.LogAsDebug(); return(new ICompletionData[0]); //the exception can happens even for the internal NRefactor-related reasons } }
static public string GetNppConfigFile() { return(Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Npp.GetConfigDir())), "config.xml")); }