Esempio n. 1
0
        private async void XmlEditor_Loading(object sender, RoutedEventArgs e)
        {
            var languages = new Monaco.LanguagesHelper(XmlEditor);

            await languages.RegisterHoverProviderAsync("xml", (model, position) =>
            {
                return(AsyncInfo.Run(async delegate(CancellationToken cancelationToken)
                {
                    // Figure out where we our in relation to our Xml Tree
                    var index = XmlEditor.Text.GetCharacterIndex((int)position.LineNumber, (int)position.Column);

                    if (index == -1)
                    {
                        return default;
                    }

                    // Get that node from the Xml Parser and wrap it in a friendly container.
                    var node = XmlSyntaxData.FromNode(_lastRoot.FindNode(index + 1), false);

                    if (node != null)
                    {
                        // Refetch proper line/col from start of token
                        var(line_s, col_s) = XmlEditor.Text.GetLineColumnIndex(node.SpanStart);
                        var(line_e, col_e) = XmlEditor.Text.GetLineColumnIndex(node.SpanEnd - 1);

                        // Provide nice UI on hover to show more info about the node.
                        return new Hover(new string[]
                        {
                            "*" + node.Type + "* " + node.Text + " [" + node.SpanStart + ".." + node.SpanEnd + ")",
                            "Line: " + line_s + " Col: " + col_s + " Length: " + node.Length
                        },
                                         new Range((uint)line_s, (uint)col_s, (uint)line_e, (uint)col_e + 1));
                    }

                    return default;
                }));
            });
Esempio n. 2
0
        private async void Editor_Loading(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(CodeContent))
            {
                CodeContent = await FileIO.ReadTextAsync(await StorageFile.GetFileFromApplicationUriAsync(new System.Uri("ms-appx:///Content.txt")));

                ButtonHighlightRange_Click(null, null);
            }

            // Ready for Code
            var languages = new Monaco.LanguagesHelper(Editor);

            var available_languages = await languages.GetLanguagesAsync();

            //Debugger.Break();

            await languages.RegisterHoverProviderAsync("csharp", new EditorHoverProvider());

            await languages.RegisterCompletionItemProviderAsync("csharp", new LanguageProvider());

            _myCondition = await Editor.CreateContextKeyAsync("MyCondition", false);

            await Editor.AddCommandAsync(KeyCode.F5, async() => {
                var md = new MessageDialog("You Hit F5!");
                await md.ShowAsync();

                // Turn off Command again.
                _myCondition?.Reset();

                // Refocus on CodeEditor
                Editor.Focus(FocusState.Programmatic);
            }, _myCondition.Key);

            await Editor.AddCommandAsync(KeyMod.CtrlCmd | KeyCode.KEY_R, async() =>
            {
                var range = await Editor.GetModel().GetFullModelRangeAsync();

                var md = new MessageDialog("Document Range: " + range.ToString());
                await md.ShowAsync();

                Editor.Focus(FocusState.Programmatic);
            });

            await Editor.AddCommandAsync(KeyMod.CtrlCmd | KeyCode.KEY_W, async() =>
            {
                var word = await Editor.GetModel().GetWordAtPositionAsync(await Editor.GetPositionAsync());

                if (word == null)
                {
                    var md = new MessageDialog("No Word Found.");
                    await md.ShowAsync();
                }
                else
                {
                    var md = new MessageDialog("Word: " + word.Word + "[" + word.StartColumn + ", " + word.EndColumn + "]");
                    await md.ShowAsync();
                }

                Editor.Focus(FocusState.Programmatic);
            });

            await Editor.AddCommandAsync(KeyMod.CtrlCmd | KeyCode.KEY_L, async() =>
            {
                var model = Editor.GetModel();
                var line  = await model.GetLineContentAsync((await Editor.GetPositionAsync()).LineNumber);
                var lines = await model.GetLinesContentAsync();
                var count = await model.GetLineCountAsync();

                var md = new MessageDialog("Current Line: " + line + "\nAll Lines [" + count + "]:\n" + string.Join("\n", lines));
                await md.ShowAsync();

                Editor.Focus(FocusState.Programmatic);
            });

            await Editor.AddCommandAsync(KeyMod.CtrlCmd | KeyCode.KEY_U, async() =>
            {
                var range = new Range(2, 10, 3, 8);
                var seg   = await Editor.GetModel().GetValueInRangeAsync(range);

                var md = new MessageDialog("Segment " + range.ToString() + ": " + seg);
                await md.ShowAsync();

                Editor.Focus(FocusState.Programmatic);
            });

            await Editor.AddActionAsync(new TestAction());
        }
        private async void Editor_Loading(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(CodeContent))
            {
                CodeContent = await FileIO.ReadTextAsync(await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Content.txt")));

                ButtonHighlightRange_Click(null, null);
            }

            // Ready for Code
            var languages = new Monaco.LanguagesHelper(Editor);

            var available_languages = await languages.GetLanguagesAsync();

            //Debugger.Break();

            await languages.RegisterHoverProviderAsync("csharp", (model, position) =>
            {
                // TODO: See if this can be internalized? Need to figure out the best pattern here to expose async method through WinRT, as can't use Task for 'async' language compatibility in WinRT Component...
                return(AsyncInfo.Run(async delegate(CancellationToken cancelationToken)
                {
                    var word = await model.GetWordAtPositionAsync(position);
                    if (word != null && word.Word != null && word.Word.IndexOf("Hit", 0, StringComparison.CurrentCultureIgnoreCase) != -1)
                    {
                        return new Hover(new string[]
                        {
                            "*Hit* - press the keys following together.",
                            "Some **more** text is here.",
                            "And a [link](https://www.github.com/)."
                        }, new Range(position.LineNumber, position.Column, position.LineNumber, position.Column + 5));
                    }

                    return default(Hover);
                }));
            });

            await languages.RegisterCompletionItemProviderAsync("csharp", new LanguageProvider());

            _myCondition = await Editor.CreateContextKeyAsync("MyCondition", false);

            await Editor.AddCommandAsync(Monaco.KeyCode.F5, async() => {
                var md = new MessageDialog("You Hit F5!");
                await md.ShowAsync();

                // Turn off Command again.
                _myCondition?.Reset();

                // Refocus on CodeEditor
                Editor.Focus(FocusState.Programmatic);
            }, _myCondition.Key);

            await Editor.AddCommandAsync(Monaco.KeyMod.CtrlCmd | Monaco.KeyCode.KEY_R, async() =>
            {
                var range = await Editor.GetModel().GetFullModelRangeAsync();

                var md = new MessageDialog("Document Range: " + range.ToString());
                await md.ShowAsync();

                Editor.Focus(FocusState.Programmatic);
            });

            await Editor.AddCommandAsync(Monaco.KeyMod.CtrlCmd | Monaco.KeyCode.KEY_W, async() =>
            {
                var word = await Editor.GetModel().GetWordAtPositionAsync(await Editor.GetPositionAsync());

                if (word == null)
                {
                    var md = new MessageDialog("No Word Found.");
                    await md.ShowAsync();
                }
                else
                {
                    var md = new MessageDialog("Word: " + word.Word + "[" + word.StartColumn + ", " + word.EndColumn + "]");
                    await md.ShowAsync();
                }

                Editor.Focus(FocusState.Programmatic);
            });

            await Editor.AddCommandAsync(Monaco.KeyMod.CtrlCmd | Monaco.KeyCode.KEY_L, async() =>
            {
                var model = Editor.GetModel();
                var line  = await model.GetLineContentAsync((await Editor.GetPositionAsync()).LineNumber);
                var lines = await model.GetLinesContentAsync();
                var count = await model.GetLineCountAsync();

                var md = new MessageDialog("Current Line: " + line + "\nAll Lines [" + count + "]:\n" + string.Join("\n", lines));
                await md.ShowAsync();

                Editor.Focus(FocusState.Programmatic);
            });

            await Editor.AddCommandAsync(Monaco.KeyMod.CtrlCmd | Monaco.KeyCode.KEY_U, async() =>
            {
                var range = new Range(2, 10, 3, 8);
                var seg   = await Editor.GetModel().GetValueInRangeAsync(range);

                var md = new MessageDialog("Segment " + range.ToString() + ": " + seg);
                await md.ShowAsync();

                Editor.Focus(FocusState.Programmatic);
            });

            await Editor.AddActionAsync(new TestAction());
        }