Пример #1
0
        private void LoadSnippets()
        {
            DataSet ds = SnippetManager.GetSnippets();

            grdToolbox.DataSource    = ds.Tables[0];
            ds.Tables[0].RowDeleted += new DataRowChangeEventHandler(ToolboxForm_RowDeleted);
            ds.Tables[0].RowChanged += new DataRowChangeEventHandler(ToolboxForm_RowChanged);
        }
Пример #2
0
        private void SetupTabSnippetHandler()
        {
            var editingKeyBindings = EditBox.TextArea.DefaultInputHandler.Editing.InputBindings.OfType <KeyBinding>();
            var tabBinding         = editingKeyBindings.Single(b => b.Key == Key.Tab && b.Modifiers == ModifierKeys.None);

            EditBox.TextArea.DefaultInputHandler.Editing.InputBindings.Remove(tabBinding);
            var newTabBinding = new KeyBinding(new SnippetTabCommand(EditBox, tabBinding.Command, SnippetManager), tabBinding.Key, tabBinding.Modifiers);

            EditBox.TextArea.DefaultInputHandler.Editing.InputBindings.Add(newTabBinding);
            SnippetManager.Initialize();
        }
Пример #3
0
 internal void InsertSnippet(bool surroundWith)
 {
     if (!MainForm.Instance.ShowLicensee)
     {
         MainForm.Instance.ActivateAutocompletion();
     }
     else
     {
         SnippetManager.ShowSnippets(this, surroundWith);
     }
 }
Пример #4
0
 public VariableSelectionVM(SnippetManager snippetManager)
 {
     this.SnippetManager = snippetManager;
     this.Variables      = new BindingList <SnippetFunction>();
     foreach (SnippetVariable variable in this.SnippetManager.Variables)
     {
         this.Variables.Add(variable);
     }
     foreach (SnippetFunction function in this.SnippetManager.Functions.Values)
     {
         this.Variables.Add(function);
     }
     _selected = this.Variables.FirstOrDefault();
 }
Пример #5
0
 public RoslynCompletionData(Document document, CompletionItem item, char?completionChar, SnippetManager snippetManager)
 {
     _document       = document;
     _item           = item;
     _completionChar = completionChar;
     _snippetManager = snippetManager;
     Text            = item.DisplayText;
     Content         = item.DisplayText;
     _glyph          = item.GetGlyph();
     if (_glyph != null)
     {
         Image = _glyph.Value.ToImageSource();
     }
 }
        public SnippetSelectionVM(SnippetManager snippetManager)
        {
            this.SnippetManager = snippetManager;
            this.Snippets       = new BindingList <Snippet>();
            foreach (Snippet snippet in this.SnippetManager.Settings.Snippets)
            {
                this.Snippets.Add(snippet);
            }
            foreach (Snippet generatorSnippet in this.SnippetManager.CodeGeneratorSnippets)
            {
                this.Snippets.Add(generatorSnippet);
            }

            this.Selected = this.Snippets.FirstOrDefault();
        }
Пример #7
0
        private void SaveAsSnippet(string filePath)
        {
            string     fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
            XNamespace ns      = "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet";
            XElement   element = new XElement((XName)(ns + "CodeSnippets"), new XElement((XName)(ns + "CodeSnippet"), new object[] { new XAttribute("Format", "1.0.0"), new XElement((XName)(ns + "Header"), new object[] { new XElement((XName)(ns + "Title"), fileNameWithoutExtension + " - assemblies and namespaces"), new XElement((XName)(ns + "Description"), fileNameWithoutExtension + " - assemblies and namespaces"), new XElement((XName)(ns + "Shortcut"), fileNameWithoutExtension.Replace(" ", "")), new XElement((XName)(ns + "SnippetTypes"), new XElement((XName)(ns + "SnippetType"), "Expansion")), this.chkNoShadow.Checked ? new XElement((XName)(ns + "Keywords"), new XElement((XName)(ns + "Keyword"), "LINQPad.NoShadowing")) : null }), new XElement((XName)(ns + "Snippet"), new object[] { new XElement((XName)(ns + "References"), from r in this.GetRefs(false).Concat <string>(this.GetRefs(true)) select new XElement((XName)(ns + "Reference"), new XElement((XName)(ns + "Assembly"), r))), new XElement((XName)(ns + "Imports"), from import in this.GetImports() select new XElement((XName)(ns + "Import"), new XElement((XName)(ns + "Namespace"), import))), new XElement((XName)(ns + "Code"), new object[] { new XAttribute("Language", "csharp"), new XCData("$end$") }) }) }));

            try
            {
                element.Save(filePath);
            }
            catch (Exception exception)
            {
                MessageBox.Show("Error saving file: " + exception.Message);
                return;
            }
            SnippetManager.ClearSnippetCache();
            SnippetManager.set_LINQPadSnippetsFolder(UserOptions.Instance.GetCustomSnippetsFolder(false));
        }
Пример #8
0
        public CodeEditor()
        {
            _codeAnalysisRunner = new JobRunner(1);

            _shell = IoC.Get <IShell>();

            _snippetManager = IoC.Get <SnippetManager>();

            _lineNumberMargin = new LineNumberMargin(this);

            _breakpointMargin = new BreakPointMargin(this, IoC.Get <IDebugManager2>().Breakpoints);

            _selectedLineBackgroundRenderer = new SelectedLineBackgroundRenderer(this);

            _selectedWordBackgroundRenderer = new SelectedWordBackgroundRenderer();

            _columnLimitBackgroundRenderer = new ColumnLimitBackgroundRenderer();

            _selectedDebugLineBackgroundRenderer = new SelectedDebugLineBackgroundRenderer();

            TextArea.TextView.Margin = new Thickness(10, 0, 0, 0);

            TextArea.TextView.BackgroundRenderers.Add(_selectedDebugLineBackgroundRenderer);
            TextArea.TextView.LineTransformers.Add(_selectedDebugLineBackgroundRenderer);

            TextArea.SelectionBrush        = Brush.Parse("#AA569CD6");
            TextArea.SelectionCornerRadius = 0;

            this.GetObservable(LineNumbersVisibleProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.LeftMargins.Add(_lineNumberMargin);
                }
                else
                {
                    TextArea.LeftMargins.Remove(_lineNumberMargin);
                }
            });

            this.GetObservable(ShowBreakpointsProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.LeftMargins.Insert(0, _breakpointMargin);
                }
                else
                {
                    TextArea.LeftMargins.Remove(_breakpointMargin);
                }
            });

            this.GetObservable(HighlightSelectedWordProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.TextView.BackgroundRenderers.Add(_selectedWordBackgroundRenderer);
                }
                else
                {
                    TextArea.TextView.BackgroundRenderers.Remove(_selectedWordBackgroundRenderer);
                }
            });

            this.GetObservable(HighlightSelectedLineProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.TextView.BackgroundRenderers.Insert(0, _selectedLineBackgroundRenderer);
                }
                else
                {
                    TextArea.TextView.BackgroundRenderers.Remove(_selectedLineBackgroundRenderer);
                }
            });

            this.GetObservable(ShowColumnLimitProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.TextView.BackgroundRenderers.Add(_columnLimitBackgroundRenderer);
                }
                else
                {
                    TextArea.TextView.BackgroundRenderers.Remove(_columnLimitBackgroundRenderer);
                }
            });

            Options = new AvaloniaEdit.TextEditorOptions
            {
                ConvertTabsToSpaces = true,
                IndentationSize     = 4
            };

            BackgroundRenderersProperty.Changed.Subscribe(s =>
            {
                if (s.Sender == this)
                {
                    if (s.OldValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IBackgroundRenderer>)s.OldValue)
                        {
                            TextArea.TextView.BackgroundRenderers.Remove(renderer);
                        }
                    }

                    if (s.NewValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IBackgroundRenderer>)s.NewValue)
                        {
                            TextArea.TextView.BackgroundRenderers.Add(renderer);
                        }
                    }
                }
            });

            DocumentLineTransformersProperty.Changed.Subscribe(s =>
            {
                if (s.Sender == this)
                {
                    if (s.OldValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IVisualLineTransformer>)s.OldValue)
                        {
                            TextArea.TextView.LineTransformers.Remove(renderer);
                        }
                    }

                    if (s.NewValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IVisualLineTransformer>)s.NewValue)
                        {
                            TextArea.TextView.LineTransformers.Add(renderer);
                        }
                    }
                }
            });

            this.GetObservable(CaretOffsetProperty).Subscribe(s =>
            {
                if (Document?.TextLength > s)
                {
                    CaretOffset = s;
                }
            });

            /*_analysisTriggerEvents.Select(_ => Observable.Timer(TimeSpan.FromMilliseconds(300)).ObserveOn(AvaloniaScheduler.Instance)
             * .SelectMany(o => DoCodeAnalysisAsync())).Switch().Subscribe(_ => { });*/

            _analysisTriggerEvents.Throttle(TimeSpan.FromMilliseconds(300)).ObserveOn(AvaloniaScheduler.Instance).Subscribe(async _ =>
            {
                await DoCodeAnalysisAsync();
            });

            this.GetObservableWithHistory(SourceFileProperty).Subscribe((file) =>
            {
                if (file.Item1 != file.Item2)
                {
                    if (System.IO.File.Exists(file.Item2.Location))
                    {
                        using (var fs = System.IO.File.OpenText(file.Item2.Location))
                        {
                            Document = new TextDocument(fs.ReadToEnd())
                            {
                                FileName = file.Item2.Location
                            };
                        }
                    }

                    _isLoaded = true;

                    RegisterLanguageService(file.Item2);

                    TextArea.TextView.Redraw();
                }
            });

            TextArea.TextEntering += (sender, e) =>
            {
                _textEntering = true;
            };

            TextArea.Caret.PositionChanged += (sender, e) =>
            {
                if (_intellisenseManager != null && !_textEntering)
                {
                    if (TextArea.Selection.IsEmpty)
                    {
                        var location = Document.GetLocation(CaretOffset);
                        _intellisenseManager.SetCursor(CaretOffset, location.Line, location.Column, UnsavedFiles.ToList());
                    }
                    else
                    {
                        var offset = Document.GetOffset(TextArea.Selection.StartPosition.Location);
                        _intellisenseManager.SetCursor(offset, TextArea.Selection.StartPosition.Line, TextArea.Selection.StartPosition.Column, UnsavedFiles.ToList());
                    }
                }

                if (CaretOffset > 0)
                {
                    var prevLocation = new TextViewPosition(Document.GetLocation(CaretOffset - 1));

                    var visualLocation    = TextArea.TextView.GetVisualPosition(prevLocation, VisualYPosition.LineBottom);
                    var visualLocationTop = TextArea.TextView.GetVisualPosition(prevLocation, VisualYPosition.LineTop);

                    var position = visualLocation - TextArea.TextView.ScrollOffset;
                    position = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                    _intellisenseControl.SetLocation(position);
                    _completionAssistantControl.SetLocation(position);

                    _selectedWordBackgroundRenderer.SelectedWord = GetWordAtOffset(CaretOffset);

                    Line              = TextArea.Caret.Line;
                    Column            = TextArea.Caret.Column;
                    EditorCaretOffset = TextArea.Caret.Offset;
                }
            };

            TextArea.TextEntered += (sender, e) =>
            {
                _intellisenseManager?.OnTextInput(e, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                _textEntering = false;
            };

            _intellisense = new IntellisenseViewModel();

            _completionAssistant = new CompletionAssistantViewModel(_intellisense);

            EventHandler <KeyEventArgs> tunneledKeyUpHandler = (send, ee) =>
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnKeyUp(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                }
            };

            EventHandler <KeyEventArgs> tunneledKeyDownHandler = (send, ee) =>
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnKeyDown(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);

                    if (ee.Key == Key.Tab && _currentSnippetContext == null && LanguageService != null)
                    {
                        var wordStart = Document.FindPrevWordStart(CaretOffset);

                        if (wordStart > 0)
                        {
                            string word = Document.GetText(wordStart, CaretOffset - wordStart);

                            var codeSnippet = _snippetManager.GetSnippet(LanguageService, SourceFile.Project?.Solution, SourceFile.Project, word);

                            if (codeSnippet != null)
                            {
                                var snippet = SnippetParser.Parse(LanguageService, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column, codeSnippet.Snippet);

                                _intellisenseManager.CloseIntellisense();

                                using (Document.RunUpdate())
                                {
                                    Document.Remove(wordStart, CaretOffset - wordStart);

                                    _intellisenseManager.IncludeSnippets = false;
                                    _currentSnippetContext = snippet.Insert(TextArea);
                                }

                                if (_currentSnippetContext.ActiveElements.Count() > 0)
                                {
                                    IDisposable disposable = null;

                                    disposable = Observable.FromEventPattern <SnippetEventArgs>(_currentSnippetContext, nameof(_currentSnippetContext.Deactivated)).Take(1).Subscribe(o =>
                                    {
                                        _currentSnippetContext = null;
                                        _intellisenseManager.IncludeSnippets = true;

                                        disposable.Dispose();
                                    });
                                }
                                else
                                {
                                    _currentSnippetContext = null;
                                    _intellisenseManager.IncludeSnippets = true;
                                }
                            }
                        }
                    }
                }
            };

            EventHandler <TextInputEventArgs> tunneledTextInputHandler = (send, ee) =>
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnTextInput(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                }
            };

            AddHandler(KeyDownEvent, tunneledKeyDownHandler, RoutingStrategies.Tunnel);
            AddHandler(KeyUpEvent, tunneledKeyUpHandler, RoutingStrategies.Tunnel);
        }
Пример #9
0
 private void SaveSnippets()
 {
     SnippetManager.SaveSnippets(((DataTable)grdToolbox.DataSource).DataSet);
 }
Пример #10
0
 private static void Run(string queryToLoad, bool runQuery, string activationCode, bool deactivate, bool noForward, bool noUpdate, string caller)
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     if (Environment.OSVersion.Version.Major >= 6)
     {
         try
         {
             SetProcessDPIAware();
         }
         catch
         {
         }
     }
     AutoSaver.Start();
     Wheeler.Register();
     if (Environment.OSVersion.Version.Major < 6)
     {
         ToolStripManager.RenderMode = ToolStripManagerRenderMode.System;
     }
     if (!((activationCode != null) || deactivate))
     {
         Splash = new LINQPad.UI.Splash();
         Splash.Show();
         Splash.Update();
     }
     Log.add_StringWriter(new Action <string>(Log.Write));
     Log.add_ExceptionWriter(new Action <Exception>(Log.Write));
     TypeResolver.ManyToOne  = Resources.ManyToOne;
     TypeResolver.OneToMany  = Resources.OneToMany;
     TypeResolver.OneToOne   = Resources.OneToOne;
     TypeResolver.ManyToMany = Resources.ManyToMany;
     TypeResolver.Results    = Resources.Results;
     TypeResolver.Column     = Resources.Column;
     TypeResolver.Key        = Resources.Key;
     TypeResolver.Database   = Resources.Database;
     TypeResolver.GetSameFolderReferences        = new Func <string, string[]>(AssemblyProber.GetSameFolderReferences);
     WSAgent.WebClientFactory                    = new Func <WebClient>(WebHelper.GetWebClient);
     WSAgent.FastWebClientFactory                = new Func <WebClient>(WebHelper.GetFastWebClient);
     WSAgent.BackupWebClientFactory              = new Func <WebClient>(WebHelper.GetBackupWebClient);
     WSAgent.CurrentVersionString                = VersionString;
     AutocompletionManager.PassiveAutocompletion = UserOptions.Instance.PassiveAutocompletion;
     AutocompletionManager.DisableLambdaSnippets = UserOptions.Instance.DisableLambdaSnippets;
     SnippetManager.set_LINQPadSnippetsFolder(UserOptions.Instance.GetCustomSnippetsFolder(false));
     WSAgent.DiagnosticMode = DiagnosticMode;
     AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Program.CurrentDomain_UnhandledException);
     if (deactivate)
     {
         WSAgent.Remove(true);
     }
     else
     {
         if (activationCode != null)
         {
             using (RegisterForm form = new RegisterForm(activationCode))
             {
                 form.ShowDialog();
                 return;
             }
         }
         if (!noUpdate)
         {
             Thread thread = new Thread(new ParameterizedThreadStart(UpdateAgent.RunServerComms))
             {
                 Name         = "Update Agent",
                 IsBackground = true
             };
             _updateThread = thread;
             _updateThread.Start(caller);
         }
         if (!string.IsNullOrEmpty(caller))
         {
             new Thread(delegate {
                 try
                 {
                     Thread.Sleep(0xbb8);
                     if (File.Exists(caller))
                     {
                         FileInfo info = new FileInfo(caller);
                         if (((info.Directory == null) || (info.Directory.Parent == null)) || (info.Directory.Parent.Name.ToLowerInvariant() != "updates"))
                         {
                             if (info.IsReadOnly)
                             {
                                 info.IsReadOnly = false;
                             }
                             if (!File.Exists(caller + ".config"))
                             {
                                 try
                                 {
                                     File.WriteAllText(caller + ".config", "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<configuration>\r\n  <startup useLegacyV2RuntimeActivationPolicy=\"true\">\r\n    <supportedRuntime version=\"v4.0\"/>\r\n  </startup>\r\n  <runtime>\r\n    <legacyUnhandledExceptionPolicy enabled=\"1\" />\r\n  </runtime>\r\n</configuration>");
                                 }
                                 catch
                                 {
                                 }
                             }
                             string path = Assembly.GetExecutingAssembly().Location + ".config";
                             if (!File.Exists(path))
                             {
                                 try
                                 {
                                     File.WriteAllText(path, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<configuration>\r\n  <startup useLegacyV2RuntimeActivationPolicy=\"true\">\r\n    <supportedRuntime version=\"v4.0\"/>\r\n  </startup>\r\n  <runtime>\r\n    <legacyUnhandledExceptionPolicy enabled=\"1\" />\r\n  </runtime>\r\n</configuration>");
                                 }
                                 catch
                                 {
                                 }
                             }
                             try
                             {
                                 File.Copy(Assembly.GetExecutingAssembly().Location, caller, true);
                             }
                             catch
                             {
                                 Thread.Sleep(0x1388);
                                 File.Copy(Assembly.GetExecutingAssembly().Location, caller, true);
                             }
                         }
                     }
                 }
                 catch
                 {
                 }
             })
             {
                 Name = "Update patcher"
             }.Start();
         }
         UnpackDb();
         QueryOneToOne();
         QueryStudioKeys(noForward);
         StartSemanticParsingService();
         new Thread(delegate {
             try
             {
                 Thread.Sleep(0x3e8);
                 if (GacResolver.IsLINQPadGaced())
                 {
                     MessageBox.Show("Warning: A different revision of LINQPad has been installed to the Global Assembly Cache. This will prevent correct operation.", "LINQPad", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                 }
             }
             catch
             {
             }
         })
         {
             IsBackground = true
         }.Start();
         Application.Run(new MainForm(queryToLoad, runQuery));
         TempFileRef.DeleteAll();
     }
 }
Пример #11
0
        public CodeEditor() : base(new TextArea(), null)
        {
            TextArea.IndentationStrategy = null;

            _shell = IoC.Get <IShell>();

            _snippetManager = IoC.Get <SnippetManager>();

            _lineNumberMargin = new LineNumberMargin(this);

            _breakpointMargin = new BreakPointMargin(this, IoC.Get <IDebugManager2>()?.Breakpoints);

            _selectedLineBackgroundRenderer    = new SelectedLineBackgroundRenderer(this);
            _bracketMatchingBackgroundRenderer = new BracketMatchingBackgroundRenderer(this);

            _selectedWordBackgroundRenderer = new SelectedWordBackgroundRenderer();

            _columnLimitBackgroundRenderer = new ColumnLimitBackgroundRenderer();

            _selectedDebugLineBackgroundRenderer = new SelectedDebugLineBackgroundRenderer();

            TextArea.TextView.Margin = new Thickness(10, 0, 0, 0);

            TextArea.TextView.BackgroundRenderers.Add(_selectedDebugLineBackgroundRenderer);
            TextArea.TextView.LineTransformers.Add(_selectedDebugLineBackgroundRenderer);
            TextArea.TextView.BackgroundRenderers.Add(_bracketMatchingBackgroundRenderer);

            TextArea.SelectionBrush        = Brush.Parse("#AA569CD6");
            TextArea.SelectionCornerRadius = 0;

            void tunneledKeyUpHandler(object send, KeyEventArgs ee)
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnKeyUp(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                }
            }

            void tunneledKeyDownHandler(object send, KeyEventArgs ee)
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnKeyDown(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);

                    if (ee.Key == Key.Tab && _currentSnippetContext == null && Editor is ICodeEditor codeEditor && codeEditor.LanguageService != null)
                    {
                        var wordStart = Document.FindPrevWordStart(CaretOffset);

                        if (wordStart > 0)
                        {
                            string word = Document.GetText(wordStart, CaretOffset - wordStart);

                            var codeSnippet = _snippetManager.GetSnippet(codeEditor.LanguageService, Editor.SourceFile.Project?.Solution, Editor.SourceFile.Project, word);

                            if (codeSnippet != null)
                            {
                                var snippet = SnippetParser.Parse(codeEditor.LanguageService, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column, codeSnippet.Snippet);

                                _intellisenseManager.CloseIntellisense();

                                using (Document.RunUpdate())
                                {
                                    Document.Remove(wordStart, CaretOffset - wordStart);

                                    _intellisenseManager.IncludeSnippets = false;
                                    _currentSnippetContext = snippet.Insert(TextArea);
                                }

                                if (_currentSnippetContext.ActiveElements.Count() > 0)
                                {
                                    IDisposable disposable = null;

                                    disposable = Observable.FromEventPattern <SnippetEventArgs>(_currentSnippetContext, nameof(_currentSnippetContext.Deactivated)).Take(1).Subscribe(o =>
                                    {
                                        _currentSnippetContext = null;
                                        _intellisenseManager.IncludeSnippets = true;

                                        disposable.Dispose();
                                    });
                                }
                                else
                                {
                                    _currentSnippetContext = null;
                                    _intellisenseManager.IncludeSnippets = true;
                                }
                            }
                        }
                    }
                }
            }

            _disposables = new CompositeDisposable {
                this.GetObservable(LineNumbersVisibleProperty).Subscribe(s =>
                {
                    if (s)
                    {
                        TextArea.LeftMargins.Add(_lineNumberMargin);
                    }
                    else
                    {
                        TextArea.LeftMargins.Remove(_lineNumberMargin);
                    }
                }),

                this.GetObservable(ShowBreakpointsProperty).Subscribe(s =>
                {
                    if (s)
                    {
                        TextArea.LeftMargins.Insert(0, _breakpointMargin);
                    }
                    else
                    {
                        TextArea.LeftMargins.Remove(_breakpointMargin);
                    }
                }),

                this.GetObservable(HighlightSelectedWordProperty).Subscribe(s =>
                {
                    if (s)
                    {
                        TextArea.TextView.BackgroundRenderers.Add(_selectedWordBackgroundRenderer);
                    }
                    else
                    {
                        TextArea.TextView.BackgroundRenderers.Remove(_selectedWordBackgroundRenderer);
                    }
                }),

                this.GetObservable(HighlightSelectedLineProperty).Subscribe(s =>
                {
                    if (s)
                    {
                        TextArea.TextView.BackgroundRenderers.Insert(0, _selectedLineBackgroundRenderer);
                    }
                    else
                    {
                        TextArea.TextView.BackgroundRenderers.Remove(_selectedLineBackgroundRenderer);
                    }
                }),

                this.GetObservable(ShowColumnLimitProperty).Subscribe(s =>
                {
                    if (s)
                    {
                        TextArea.TextView.BackgroundRenderers.Add(_columnLimitBackgroundRenderer);
                    }
                    else
                    {
                        TextArea.TextView.BackgroundRenderers.Remove(_columnLimitBackgroundRenderer);
                    }
                }),

                this.GetObservable(ColumnLimitProperty).Subscribe(limit =>
                {
                    _columnLimitBackgroundRenderer.Column = limit;
                    this.TextArea.TextView.InvalidateLayer(KnownLayer.Background);
                }),

                this.GetObservable(ContextActionsIconProperty).Subscribe(icon =>
                {
                    if (_contextActionsRenderer != null)
                    {
                        _contextActionsRenderer.IconImage = icon;
                    }
                }),

                this.GetObservable(ColorSchemeProperty).Subscribe(colorScheme =>
                {
                    if (colorScheme != null)
                    {
                        Background = colorScheme.Background;
                        Foreground = colorScheme.Text;

                        _lineNumberMargin.Background = colorScheme.Background;

                        if (_diagnosticMarkersRenderer != null)
                        {
                            _diagnosticMarkersRenderer.ColorScheme = colorScheme;
                        }

                        _textColorizer?.RecalculateBrushes();
                        TextArea.TextView.InvalidateLayer(KnownLayer.Background);
                        TextArea.TextView.Redraw();
                    }
                }),

                this.GetObservable(EditorCaretOffsetProperty).Subscribe(s =>
                {
                    if (Document?.TextLength >= s)
                    {
                        CaretOffset = s;
                        TextArea.Caret.BringCaretToView();
                    }
                }),

                BackgroundRenderersProperty.Changed.Subscribe(s =>
                {
                    if (s.Sender == this)
                    {
                        if (s.OldValue != null)
                        {
                            foreach (var renderer in (ObservableCollection <IBackgroundRenderer>)s.OldValue)
                            {
                                TextArea.TextView.BackgroundRenderers.Remove(renderer);
                            }
                        }

                        if (s.NewValue != null)
                        {
                            foreach (var renderer in (ObservableCollection <IBackgroundRenderer>)s.NewValue)
                            {
                                TextArea.TextView.BackgroundRenderers.Add(renderer);
                            }
                        }
                    }
                }),

                DocumentLineTransformersProperty.Changed.Subscribe(s =>
                {
                    if (s.Sender == this)
                    {
                        if (s.OldValue != null)
                        {
                            foreach (var renderer in (ObservableCollection <IVisualLineTransformer>)s.OldValue)
                            {
                                TextArea.TextView.LineTransformers.Remove(renderer);
                            }
                        }

                        if (s.NewValue != null)
                        {
                            foreach (var renderer in (ObservableCollection <IVisualLineTransformer>)s.NewValue)
                            {
                                TextArea.TextView.LineTransformers.Add(renderer);
                            }
                        }
                    }
                }),

                Observable.FromEventPattern(TextArea.Caret, nameof(TextArea.Caret.PositionChanged)).Subscribe(e =>
                {
                    if (_isLoaded && Document != null)
                    {
                        _lastLine = TextArea.Caret.Line;

                        Line              = TextArea.Caret.Line;
                        Column            = TextArea.Caret.Column;
                        EditorCaretOffset = TextArea.Caret.Offset;

                        var location = new TextViewPosition(Document.GetLocation(CaretOffset));

                        var visualLocation    = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineBottom);
                        var visualLocationTop = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineTop);

                        var position = visualLocation - TextArea.TextView.ScrollOffset;
                        position     = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                        _intellisenseControl.SetLocation(position);
                    }
                }),

                Observable.FromEventPattern(TextArea.Caret, nameof(TextArea.Caret.PositionChanged)).Throttle(TimeSpan.FromMilliseconds(100)).ObserveOn(AvaloniaScheduler.Instance).Subscribe(e =>
                {
                    if (Document != null)
                    {
                        var location = new TextViewPosition(Document.GetLocation(CaretOffset));

                        if (_intellisenseManager != null && !_textEntering)
                        {
                            if (TextArea.Selection.IsEmpty)
                            {
                                _intellisenseManager.SetCursor(CaretOffset, location.Line, location.Column, UnsavedFiles.ToList());
                            }
                            else if (_currentSnippetContext != null)
                            {
                                var offset = Document.GetOffset(TextArea.Selection.StartPosition.Location);
                                _intellisenseManager.SetCursor(offset, TextArea.Selection.StartPosition.Line, TextArea.Selection.StartPosition.Column, UnsavedFiles.ToList());
                            }
                        }

                        _selectedWordBackgroundRenderer.SelectedWord = GetWordAtOffset(CaretOffset);

                        TextArea.TextView.InvalidateLayer(KnownLayer.Background);
                    }
                }),

                this.WhenAnyValue(x => x.DebugHighlight).Where(loc => loc != null).Subscribe(location =>
                {
                    if (location.Line != -1)
                    {
                        SetDebugHighlight(location.Line, location.StartColumn, location.EndColumn);
                    }
                    else
                    {
                        ClearDebugHighlight();
                    }
                }),
                this.GetObservable(EditorProperty).Subscribe(editor =>
                {
                    if (editor != null)
                    {
                        if (editor.SourceFile.Project?.Solution != null)
                        {
                            _snippetManager.InitialiseSnippetsForSolution(editor.SourceFile.Project.Solution);
                        }

                        if (editor.SourceFile.Project != null)
                        {
                            _snippetManager.InitialiseSnippetsForProject(editor.SourceFile.Project);
                        }

                        SyntaxHighlighting = CustomHighlightingManager.Instance.GetDefinition(editor.SourceFile.ContentType);

                        if (editor.Document is AvalonStudioTextDocument td && Document != td.Document)
                        {
                            Document = td.Document;

                            if (editor.Offset <= Document.TextLength)
                            {
                                CaretOffset = editor.Offset;
                            }

                            _textColorizer = new TextColoringTransformer(Document);
                            _scopeLineBackgroundRenderer = new ScopeLineBackgroundRenderer(Document);


                            TextArea.TextView.BackgroundRenderers.Add(_scopeLineBackgroundRenderer);
                            TextArea.TextView.LineTransformers.Insert(0, _textColorizer);

                            _diagnosticMarkersRenderer = new TextMarkerService(Document);
                            _contextActionsRenderer    = new ContextActionsRenderer(this, _diagnosticMarkersRenderer);
                            TextArea.LeftMargins.Add(_contextActionsRenderer);
                            TextArea.TextView.BackgroundRenderers.Add(_diagnosticMarkersRenderer);
                        }

                        if (editor is ICodeEditor codeEditor)
                        {
                            if (codeEditor.Highlights != null)
                            {
                                _disposables.Add(
                                    Observable.FromEventPattern <NotifyCollectionChangedEventArgs>(codeEditor.Highlights, nameof(codeEditor.Highlights.CollectionChanged))
                                    .Subscribe(observer =>
                                {
                                    var e = observer.EventArgs;

                                    switch (e.Action)
                                    {
                                    case NotifyCollectionChangedAction.Add:
                                        foreach (var(tag, highlightList) in  e.NewItems.Cast <(object tag, SyntaxHighlightDataList highlightList)>())
                                        {
                                            _textColorizer.SetTransformations(tag, highlightList);
                                        }
                                        break;

                                    case NotifyCollectionChangedAction.Remove:
                                        foreach (var(tag, highlightList) in  e.OldItems.Cast <(object tag, SyntaxHighlightDataList highlightList)>())
                                        {
                                            _textColorizer.RemoveAll(i => i.Tag == tag);
                                        }
                                        break;

                                    case NotifyCollectionChangedAction.Reset:
                                        foreach (var(tag, highlightList) in  e.OldItems.Cast <(object tag, SyntaxHighlightDataList highlightList)>())
                                        {
                                            _textColorizer.RemoveAll(i => true);
                                        }
                                        break;

                                    default:
                                        throw new NotSupportedException();
                                    }

                                    TextArea.TextView.Redraw();
                                }));

                                _disposables.Add(
                                    Observable.FromEventPattern <NotifyCollectionChangedEventArgs>(codeEditor.Diagnostics, nameof(codeEditor.Diagnostics.CollectionChanged))
                                    .Subscribe(observer =>
                                {
                                    var e = observer.EventArgs;

                                    switch (e.Action)
                                    {
                                    case NotifyCollectionChangedAction.Add:
                                        foreach (var(tag, diagnostics) in  e.NewItems.Cast <(object tag, IEnumerable <Diagnostic> diagnostics)>())
                                        {
                                            _diagnosticMarkersRenderer.SetDiagnostics(tag, diagnostics);
                                        }
                                        break;

                                    case NotifyCollectionChangedAction.Remove:
                                        foreach (var(tag, diagnostics) in  e.OldItems.Cast <(object tag, IEnumerable <Diagnostic> diagnostics)>())
                                        {
                                            _diagnosticMarkersRenderer.RemoveAll(x => x.Tag == tag);
                                        }
                                        break;

                                    case NotifyCollectionChangedAction.Reset:
                                        foreach (var(tag, diagnostics) in  e.OldItems.Cast <(object tag, IEnumerable <Diagnostic> diagnostics)>())
                                        {
                                            _diagnosticMarkersRenderer.RemoveAll(i => true);
                                        }
                                        break;

                                    default:
                                        throw new NotSupportedException();
                                    }

                                    TextArea.TextView.Redraw();
                                    _contextActionsRenderer.OnDiagnosticsUpdated();
                                }));

                                _disposables.Add(codeEditor.WhenAnyValue(x => x.CodeIndex).Subscribe(codeIndex =>
                                {
                                    _scopeLineBackgroundRenderer.ApplyIndex(codeIndex);
                                }));

                                _scopeLineBackgroundRenderer.ApplyIndex(codeEditor.CodeIndex);

                                foreach (var(tag, diagnostics) in codeEditor.Diagnostics)
                                {
                                    _diagnosticMarkersRenderer.SetDiagnostics(tag, diagnostics);
                                }

                                foreach (var(tag, highlights) in codeEditor.Highlights)
                                {
                                    _textColorizer.SetTransformations(tag, highlights);
                                }

                                TextArea.TextView.Redraw();
                            }

                            _intellisenseManager = new IntellisenseManager(editor, Intellisense, _completionAssistant, codeEditor.LanguageService, editor.SourceFile, offset =>
                            {
                                var location = new TextViewPosition(Document.GetLocation(offset));

                                var visualLocation    = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineBottom);
                                var visualLocationTop = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineTop);

                                var position = visualLocation - TextArea.TextView.ScrollOffset;
                                position     = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                                _completionAssistantControl.SetLocation(position);
                            });

                            _disposables.Add(_intellisenseManager);

                            foreach (var contextActionProvider in codeEditor.LanguageService.GetContextActionProviders())
                            {
                                _contextActionsRenderer.Providers.Add(contextActionProvider);
                            }
                        }

                        Dispatcher.UIThread.Post(() =>
                        {
                            TextArea.ScrollToLine(Line);
                            Focus();
                        });
                    }
                    else
                    {
                        if (Document != null)
                        {
                            Document = null;
                        }
                    }
                }),
                this.GetObservable(RenameOpenProperty).Subscribe(open =>
                {
                    if (_isLoaded && Editor != null)
                    {
                        var token    = Editor.Document.GetToken(CaretOffset);
                        var location = new TextViewPosition(Document.GetLocation(token.Offset));

                        var visualLocation    = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineBottom);
                        var visualLocationTop = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineTop);

                        var position = visualLocation - TextArea.TextView.ScrollOffset;
                        position     = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                        _renameControl.SetLocation(position);
                        _renameControl.Open(this, Editor.Document.GetText(token));
                    }
                }),

                AddHandler(KeyDownEvent, tunneledKeyDownHandler, RoutingStrategies.Tunnel),
                AddHandler(KeyUpEvent, tunneledKeyUpHandler, RoutingStrategies.Tunnel)
            };

            Options = new AvaloniaEdit.TextEditorOptions
            {
                ConvertTabsToSpaces   = true,
                IndentationSize       = 4,
                EnableHyperlinks      = false,
                EnableEmailHyperlinks = false,
            };

            //BackgroundRenderersProperty.Changed.Subscribe(s =>
            //{
            //    if (s.Sender == this)
            //    {
            //        if (s.OldValue != null)
            //        {
            //            foreach (var renderer in (ObservableCollection<IBackgroundRenderer>)s.OldValue)
            //            {
            //                TextArea.TextView.BackgroundRenderers.Remove(renderer);
            //            }
            //        }

            //        if (s.NewValue != null)
            //        {
            //            foreach (var renderer in (ObservableCollection<IBackgroundRenderer>)s.NewValue)
            //            {
            //                TextArea.TextView.BackgroundRenderers.Add(renderer);
            //            }
            //        }
            //    }
            //});

            //DocumentLineTransformersProperty.Changed.Subscribe(s =>
            //{
            //    if (s.Sender == this)
            //    {
            //        if (s.OldValue != null)
            //        {
            //            foreach (var renderer in (ObservableCollection<IVisualLineTransformer>)s.OldValue)
            //            {
            //                TextArea.TextView.LineTransformers.Remove(renderer);
            //            }
            //        }

            //        if (s.NewValue != null)
            //        {
            //            foreach (var renderer in (ObservableCollection<IVisualLineTransformer>)s.NewValue)
            //            {
            //                TextArea.TextView.LineTransformers.Add(renderer);
            //            }
            //        }
            //    }
            //});


            /*_analysisTriggerEvents.Select(_ => Observable.Timer(TimeSpan.FromMilliseconds(300)).ObserveOn(AvaloniaScheduler.Instance)
             * .SelectMany(o => DoCodeAnalysisAsync())).Switch().Subscribe(_ => { });*/

            Intellisense = new IntellisenseViewModel();

            _completionAssistant = new CompletionAssistantViewModel(Intellisense);

            TextArea.TextEntering += TextArea_TextEntering;

            TextArea.TextEntered += TextArea_TextEntered;
        }

        ~CodeEditor()
        {
        }
        public IEnumerable<SnippetDTO> FindSnippetsByUserPaged(string text, int userId, Guid userGuid,
                                                               int snippetsPerPage, int currentPage = 1)
        {
            var mgr = new SnippetManager(SearchEngine, UserOperationsEngine, UserValidationEngine,
                                         SnippetOperationsEngine);

            IEnumerable<SnippetDTO> result = mgr.GetPagedSnippetList(snippetsPerPage, currentPage, userId, userGuid);

            return result.GroupBy(i => i.Id, (key, group) => group.First()).ToArray();
        }
        /// <summary>
        /// Returns all public snippets and private snippets that match the input text
        /// </summary>
        /// <param name="text"></param>
        /// <param name="userId"></param>
        /// <param name="userGuid"></param>
        /// <returns></returns>
        public IEnumerable<SnippetDTO> FindSnippetsByUser(string text, int userId, Guid userGuid)
        {
            var mgr = new SnippetManager(SearchEngine, UserOperationsEngine, UserValidationEngine,
                                         SnippetOperationsEngine);

            IEnumerable<SnippetDTO> result = mgr.PerformSearch(text, userId, userGuid).ToSnippetDTO();

            return result.GroupBy(i => i.Id, (key, group) => group.First()).ToArray();
        }