Ejemplo n.º 1
0
        //private IProject GetProjectFromTextView(ITextView textView)
        //{
        //    IProject project = Bridge.Get<IBProjects>().ProjectGlobal;

        //    if (String.IsNullOrEmpty(textView.CurrentFilePath))
        //    {
        //        if (textView is IProjectTarget)
        //        {
        //            project = (textView as IProjectTarget).GetProject();
        //        }
        //    }
        //    else
        //    {
        //        project = Bridge.Get().GetSystemNode(textView.CurrentFilePath).GetProject(true);
        //    }

        //    return project;
        //}

        public void ProcessInline(ITextView textView, int textColumnIndex)
        {
            if (String.IsNullOrEmpty(textView.CurrentFilePath))
            {
                textView.Settings.Notifier.Info(Strings.TextControl_TextTemplates_Title, Strings.TextControl_TextTemples_TextFileMustBeSaved);
                return;
            }

            var tokens = new List <Token>(this.GetTokens(textView));

            if (textView.GetTextStyle("TemplateToken") == null)
            {
                // Add the text style if it does not already exist.
                textView.AddStyle(
                    new TextStyleManual(
                        "Template Token",
                        "TemplateToken",
                        "The default text style for the text",
                        Color.Empty, Color.LawnGreen,
                        null,
                        TextStyleDisplayMode.Always,
                        TextStylePaintMode.Inline));
            }

            var lastSelectedIdx = textView.SelectionStart;
            var start           = lastSelectedIdx;

            textView.SelectionLength = 0;
            const string tokenPlaceholder = "[?]";

            foreach (var t in tokens)
            {
                if (t.Type.IsFreetext)
                {
                    var processResult = t.Process();
                    textView.TextInsert(textView.SelectionStart, processResult);
                    textView.SelectionStart += processResult.Length;
                    lastSelectedIdx         += processResult.Length;
                }
                else
                {
                    var placeholderStart = textView.SelectionStart;
                    textView.TextInsert(textView.SelectionStart, tokenPlaceholder);
                    textView.SelectionStart += tokenPlaceholder.Length;

                    var segment = textView.TextDocument.CreateStyledTextSegment(textView.GetTextStyle("TemplateToken"));

                    segment.Index = placeholderStart - textView.GetFirstCharIndexFromLine(textView.GetLineFromCharIndex(placeholderStart));
                    segment.SetLength(textColumnIndex, tokenPlaceholder.Length);
                    segment.Object = t;

                    // Add the style to the text.
                    textView.TextDocument.TextSegmentStyledManager.AddManualTextSegment(segment, placeholderStart, textColumnIndex);
                }
            }

            while (true)
            {
                var foundTemplateTokens = 0;
                foreach (var style in textView.TextDocument.TextSegmentStyledManager.GetStyledTextSegments("TemplateToken"))
                {
                    foundTemplateTokens++;

                    var t = style.Object as Token;

                    var idx = style.IndexGlobal;
                    textView.SelectionStart  = idx;
                    textView.SelectionLength = style.GetLength(textColumnIndex);

                    var tokenResult = t.Process();

                    textView.SelectionLength = 0;

                    textView.TextRemove(idx, style.GetLength(textColumnIndex));
                    textView.TextInsert(idx, tokenResult);

                    // Fake the key as finalizing, since we are swapping out the content dynamically
                    // and hence never actually sending the "\n" character which would recheck for text segments.
                    textView.TextDocument.FakeFinalizingKey(idx, textColumnIndex);
                    lastSelectedIdx += tokenResult.Length;

                    // Redo it all from the beginning, since otherwise we will get a "enumeration was modified" exception.
                    break;
                }

                if (foundTemplateTokens == 0)
                {
                    break;
                }
            }

            textView.SelectionStart = lastSelectedIdx;

            // Invalidate the painting of the textview.
            textView.Invalidate();
        }