Пример #1
0
        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);
            }
        }
Пример #2
0
        static public bool NavigateToNextParam(SnippetContext context)
        {
            var indicators = Npp.FindIndicatorRanges(SnippetContext.indicatorId);

            if (!indicators.Any())
            {
                return(false);
            }

            Point  currentParam             = context.CurrentParameter.Value;
            string currentParamOriginalText = context.CurrentParameterValue;

            Npp.SetSelection(currentParam.X, currentParam.X);
            string currentParamDetectedText = Npp.GetWordAtCursor("\t\n\r ,;'\"".ToCharArray());


            if (currentParamOriginalText != currentParamDetectedText)
            {
                //current parameter is modified, indicator is destroyed so restore the indicator first
                Npp.SetIndicatorStyle(SnippetContext.indicatorId, SciMsg.INDIC_BOX, Color.Blue);
                Npp.PlaceIndicator(SnippetContext.indicatorId, currentParam.X, currentParam.X + currentParamDetectedText.Length);

                indicators = Npp.FindIndicatorRanges(SnippetContext.indicatorId);//needs refreshing as the document is modified

                var paramsInfo = indicators.Select(p => new
                {
                    Index = indicators.IndexOf(p),
                    Text  = Npp.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 = Npp.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 => Npp.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(Npp.GetTextBetween(range)))
                {
                    nextParameter = range;
                    break;
                }
            }

            if (!nextParameter.HasValue)
            {
                nextParameter = indicators.FirstOrDefault();
            }

            context.CurrentParameter = nextParameter;
            if (context.CurrentParameter.HasValue)
            {
                Npp.SetSelection(context.CurrentParameter.Value.X, context.CurrentParameter.Value.Y);
                context.CurrentParameterValue = Npp.GetTextBetween(context.CurrentParameter.Value);
            }

            return(true);
        }