Пример #1
0
        public static SnippetContext PrepareForIncertion(string rawText, int charsOffset, int documentOffset = 0)
        {
            var retval = new SnippetContext();

            retval.ReplacementString = rawText;

            string offset = new string(' ', charsOffset);

            retval.ReplacementString = retval.ReplacementString.Replace(Environment.NewLine, Environment.NewLine + offset);

            int endPos   = -1;
            int startPos = retval.ReplacementString.IndexOf("$");

            while (startPos != -1)
            {
                endPos = retval.ReplacementString.IndexOf("$", startPos + 1);

                if (endPos != -1)
                {
                    //'$item$' -> 'item'
                    int newEndPos = endPos - 2;

                    if (!SimpleSnippetInsertion || retval.ReplacementString.Substring(startPos, 3) == "$|$")
                    {
                        retval.Parameters.Add(new Point(startPos + documentOffset, newEndPos + 1 + documentOffset));
                    }

                    string leftText       = retval.ReplacementString.Substring(0, startPos);
                    string rightText      = retval.ReplacementString.Substring(endPos + 1);
                    string placementValue = retval.ReplacementString.Substring(startPos + 1, endPos - startPos - 1);

                    retval.ReplacementString = leftText + placementValue + rightText;

                    endPos = newEndPos;
                }
                else
                {
                    break;
                }

                startPos = retval.ReplacementString.IndexOf("$", endPos + 1);
            }

            if (retval.Parameters.Any())
            {
                retval.CurrentParameter = retval.Parameters.FirstOrDefault();
            }

            return(retval);
        }
Пример #2
0
        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);
        }