public static bool TriggerCodeSnippetInsertion() { if (InsertionActive) { return(false); // do no insert a snippet within a snippet! } string token = Sci.GetWord(null, Sci.CurrentPosition); var curPos = Sci.CurrentPosition; Point tokenPoints = new Point(curPos - token.Length, curPos); if (Contains(token)) { string replacement = GetTemplate(token); if (replacement != null) { int line = Sci.Line.CurrentLine; int lineStartPos = Sci.GetLine(line).Position; int horizontalOffset = tokenPoints.X - lineStartPos; //relative selection in the replacement text PrepareForIncertion(replacement, horizontalOffset, tokenPoints.X); var indic = Sci.GetIndicator(SnippetContext.IndicatorId); indic.IndicatorStyle = IndicatorStyle.Box; indic.ForeColor = Color.Blue; foreach (var point in LocSnippetContext.Parameters) { indic.Add(point.X, point.Y); } if (LocSnippetContext.CurrentParameter.HasValue) { Sci.SetSelection(LocSnippetContext.CurrentParameter.Value.X, LocSnippetContext.CurrentParameter.Value.Y); LocSnippetContext.CurrentParameterValue = Sci.GetTextBetween(LocSnippetContext.CurrentParameter.Value); } AutoCompletion.Cloak(); if (LocSnippetContext.Parameters.Count <= 1) { FinalizeCurrent(); } } return(true); } return(false); }
static public void FinalizeCurrent() { var indic = Sci.GetIndicator(SnippetContext.IndicatorId); var indicators = indic.FindRanges().ToArray(); foreach (var range in indicators) { indic.Clear(range.X, range.Y); } var caretPoint = indicators.Where(point => { string text = Sci.GetTextBetween(point); return(text == " " || text == "|"); }) .FirstOrDefault(); if (caretPoint.X != caretPoint.Y) { Sci.SetTextByRange(caretPoint.X, caretPoint.Y, ""); Sci.SetSelection(caretPoint.X, caretPoint.X); } LocSnippetContext = null; }
static public bool NavigateToNextParam() { var indic = Sci.GetIndicator(SnippetContext.IndicatorId); indic.IndicatorStyle = IndicatorStyle.Box; indic.ForeColor = Color.Blue; var indicators = indic.FindRanges().ToArray(); if (!indicators.Any()) { return(false); } if (LocSnippetContext.CurrentParameter != null) { Point currentParam = LocSnippetContext.CurrentParameter.Value; string currentParamOriginalText = LocSnippetContext.CurrentParameterValue; Sci.SetSelection(currentParam.X, currentParam.X); string currentParamDetectedText = Sci.GetWordAtPosition(Sci.CurrentPosition, AutoCompletion.CurrentLangAdditionalChars); if (currentParamOriginalText != currentParamDetectedText) { //current parameter is modified, indicator is destroyed so restore the indicator first indic.Add(currentParam.X, currentParam.X + currentParamDetectedText.Length); indicators = indic.FindRanges().ToArray(); //needs refreshing as the document is modified var paramsInfo = indicators.Select(p => new { Index = indicators.IndexOf(p), Text = Sci.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) { ReplaceTextAtIndicator(currentParamDetectedText, indicators[param.Index]); indicators = indic.FindRanges().ToArray(); //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 => Sci.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(Sci.GetTextBetween(range))) { nextParameter = range; break; } } if (!nextParameter.HasValue) { nextParameter = indicators.FirstOrDefault(); } LocSnippetContext.CurrentParameter = nextParameter; } if (LocSnippetContext.CurrentParameter.HasValue) { Sci.SetSelection(LocSnippetContext.CurrentParameter.Value.X, LocSnippetContext.CurrentParameter.Value.Y); LocSnippetContext.CurrentParameterValue = Sci.GetTextBetween(LocSnippetContext.CurrentParameter.Value); } return(true); }