Esempio n. 1
0
        /// <summary>
        /// Sets the editor options.
        /// </summary>
        /// <param name="editor">The editor.</param>
        protected virtual void SetEditorOptions(IronyModManager.Controls.TextEditor editor)
        {
            var ctx = new ContextMenu
            {
                Items = new List <MenuItem>()
                {
                    new MenuItem()
                    {
                        Header  = ViewModel.EditorCopy,
                        Command = ReactiveCommand.Create(() => editor.Copy()).DisposeWith(Disposables)
                    },
                    new MenuItem()
                    {
                        Header  = ViewModel.EditorCut,
                        Command = ReactiveCommand.Create(() => editor.Cut()).DisposeWith(Disposables)
                    },
                    new MenuItem()
                    {
                        Header  = ViewModel.EditorPaste,
                        Command = ReactiveCommand.Create(() => editor.Paste()).DisposeWith(Disposables)
                    },
                    new MenuItem()
                    {
                        Header  = ViewModel.EditorDelete,
                        Command = ReactiveCommand.Create(() => editor.Delete()).DisposeWith(Disposables)
                    },
                    new MenuItem()
                    {
                        Header  = ViewModel.EditorSelectAll,
                        Command = ReactiveCommand.Create(() => editor.SelectAll()).DisposeWith(Disposables)
                    },
                    new MenuItem()
                    {
                        Header = "-"
                    },
                    new MenuItem()
                    {
                        Header  = ViewModel.EditorUndo,
                        Command = ReactiveCommand.Create(() => editor.Undo()).DisposeWith(Disposables)
                    },
                    new MenuItem()
                    {
                        Header  = ViewModel.EditorRedo,
                        Command = ReactiveCommand.Create(() => editor.Redo()).DisposeWith(Disposables)
                    }
                }
            };

            editor.ContextMenu = ctx;
            editor.Options     = new TextEditorOptions()
            {
                ConvertTabsToSpaces = true,
                IndentationSize     = 4
            };
            editor.TextArea.ActiveInputHandler = new Implementation.AvaloniaEdit.TextAreaInputHandler(editor);

            ViewModel.WhenAnyValue(p => p.EditingYaml).Subscribe(s =>
            {
                setEditMode();
            }).DisposeWith(Disposables);
            setEditMode();

            void setEditMode()
            {
                if (ViewModel.EditingYaml)
                {
                    editor.SyntaxHighlighting = resourceLoader.GetYAMLDefinition();
                }
                else
                {
                    editor.SyntaxHighlighting = resourceLoader.GetPDXScriptDefinition();
                }
            }

            bool manualAppend = false;

            editor.TextChanged += (sender, args) =>
            {
                // It's a hack I know see: https://github.com/AvaloniaUI/AvaloniaEdit/issues/99.
                // I'd need to go into the code to fix it and it ain't worth it. There doesn't seem to be any feedback on this issue as well.
                var lines = editor.Text.SplitOnNewLine(false).ToList();
                if (lines.Count > 3)
                {
                    if (manualAppend)
                    {
                        manualAppend = false;
                        return;
                    }
                    var carretOffset = editor.CaretOffset;
                    for (int i = 1; i <= 3; i++)
                    {
                        var last = lines[^ i];