Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Document Symbols? #87

Open
IvanJosipovic opened this issue Mar 27, 2023 · 0 comments
Open

How to get Document Symbols? #87

IvanJosipovic opened this issue Mar 27, 2023 · 0 comments

Comments

@IvanJosipovic
Copy link

IvanJosipovic commented Mar 27, 2023

I'm looking for the OutlineModel.asListOfDocumentSymbols function. See here, microsoft/monaco-editor#2959

JS code example:

import { editor } from 'monaco-editor';
import { ILanguageFeaturesService } from 'monaco-editor/esm/vs/editor/common/services/languageFeatures.js';
import { OutlineModel } from 'monaco-editor/esm/vs/editor/contrib/documentSymbols/browser/outlineModel.js';
import { StandaloneServices } from 'monaco-editor/esm/vs/editor/standalone/browser/standaloneServices.js';

const ed = editor.create(/* options */);

async function main() {
  const { documentSymbolProvider } = StandaloneServices.get(ILanguageFeaturesService);
  const outline = await OutlineModel.create(documentSymbolProvider, ed.getModel());
  const symbols = outline.asListOfDocumentSymbols();
}

This is useful for the registerHoverProvider which I have implemented with the code below:

namespace UI.Components
{
    public partial class MonacoRegisterHoverProvider
    {
        [Inject]
        IJSRuntime jsRuntime { get; set; }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                var module = await jsRuntime.InvokeAsync<IJSObjectReference>("import", $"./_content/UI/Components/{GetType().Name}.razor.js");
                await module.InvokeVoidAsync("registerHoverProvider", "yaml", DotNetObjectReference.Create(this));
            }
        }

        [JSInvokable]
        public async Task<object> ProvideHoverAsync(string model, Position position)
        {
            return new {
                contents = new[]
                {
                    new
                    {
                        value = $"This is a tooltip at {position.lineNumber} - {position.column}"
                    }
                }
            };
        }
    }

    public class Position
    {
        public int lineNumber { get; set; }
        public int column { get; set; }
    }
}
export function registerHoverProvider(languageId, hoverProvider) {
    monaco.languages.registerHoverProvider(languageId, {
        provideHover: function (model, position) {
            const yamlContent = model.getValueInRange({
                startLineNumber: 1,
                endLineNumber: 3,
            });

            return hoverProvider.invokeMethodAsync("ProvideHoverAsync", yamlContent, position);
        }
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant