question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`getDocumentSymbols` returns incorrect ordered symbol list in flatten mode

See original GitHub issue

monaco-editor version: 0.24.0 **Browser: Chrome 91.0.4472.77 **OS: macOS 12.0 Beta Playground code that reproduces the issue:

https://alauda.github.io/k8s-form-in-action/demo/

Change the yaml content like the following:

containers:
  - name: nginx
    ports:
      - containerPort: 80

open devtool, execute window.DEBUG = true, click the yaml content you will get

image

As you can see, name symbol comes before 0 which is incorrect obviously, while containerPort comes after 0 which is correct.

What means when a symbol has multi children, the flatted result is incorrect.

If you want to run this sample locally, you can check https://github.com/alauda/k8s-form-in-action

run yarn && yarn dev and then visit http://localhost:4200

The related source code is at https://github.com/alauda/k8s-form-in-action/blob/master/src/app/demo/component.ts#L168

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rcjsuencommented, Jul 2, 2021

I did some digging into this…

const value =
    `containers:
  - name: nginx
    ports:
      - containerPort: 80`;

monaco.editor.create(document.getElementById("container"), {
    value: value,
    language: "yaml"
});

const symbols = [
    {
        name: "containers",
        range: {
            startLineNumber: 1, startColumn: 1,
            endLineNumber: 4, endColumn: 26
        }
    },
    {
        name: "container 0",
        range: {
            startLineNumber: 2, startColumn: 5,
            endLineNumber: 5, endColumn: 1
        },
    },
    {
        name: "name",
        range: {
            startLineNumber: 2, startColumn: 5,
            endLineNumber: 2, endColumn: 16
        }
    },
    {
        name: "ports",
        range: {
            startLineNumber: 3, startColumn: 5,
            endLineNumber: 5, endColumn: 1
        }
    },
    {
        name: "ports 0",
        range: {
            startLineNumber: 4, startColumn: 9,
            endLineNumber: 4, endColumn: 26
        }

    },
    {
        name: "containerPort",
        range: {
            startLineNumber: 4, startColumn: 9,
            endLineNumber: 4, endColumn: 26
        }
    }
]

monaco.languages.registerDocumentSymbolProvider("yaml", {
    provideDocumentSymbols: (model, token) => {
        return symbols.map(s => {
            return {
                name: s.name,
                range: s.range,
                selectionRange: s.range,
                kind: monaco.languages.SymbolKind.Object
            }
        });
    }
});

image

  1. You can reproduce this issue by running the above JavaScript code in the Monaco Editor Playground. Use F1 in the editor and use @ to jump to the symbol. In my screenshot I’ve added some whitespace after creating the editor so you can see both the code and the list of symbols for illustrative purposes.
const ranges = [
    {startLineNumber: 1, startColumn: 1, endLineNumber: 5, endColumn: 1}, // containers
    {startLineNumber: 2, startColumn: 5, endLineNumber: 5, endColumn: 1}, // 0-th element of containers
    {startLineNumber: 2, startColumn: 5, endLineNumber: 2, endColumn: 16}, // name
    {startLineNumber: 3, startColumn: 5, endLineNumber: 5, endColumn: 1}, // ports
    {startLineNumber: 4, startColumn: 9, endLineNumber: 4, endColumn: 26}, // 0-th element of ports
    {startLineNumber: 4, startColumn: 9, endLineNumber: 4, endColumn: 26}, // containerPort
]
ranges.sort((a, b) => {
    return monaco.Range.compareRangesUsingStarts(a, b);
});
console.log(ranges);
  1. The reason the symbols are getting “moved around” is because of how VS Code is sorting them in compareRangesUsingStarts(*). You can see this change by running the above JavaScript code in the Monaco Editor Playground. The calling code can be found here and you can keep walking backwards from there as desired.
  2. @JounQin I should note to you that the getDocumentSymbols(*) function is not considered API based on the contents of the vscode.d.ts file or the monaco.d.ts file so you shouldn’t be using it to get an outline of the symbols from a model.
1reaction
spahnkecommented, Jun 9, 2021

Can’t offer much help here, but as far as I know the Monaco editor does not provide any language intelligence for YAML out-of-the-box. So where are these symbols computed? Where does the DocumentSymbolProvider come from? I would start there and look into the ordering coming directly from that provider. That would probably also help you in providing a minimal repro if there should be a bug on Monaco’s side.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flatten Nested List Iterator - LeetCode
Flatten Nested List Iterator - You are given a nested list of integers ... next repeatedly until hasNext returns false, the order of...
Read more >
https://raw.githubusercontent.com/emacs-lsp/lsp-mo...
Each entry in the list can be either: a symbol, the server-id for the LSP client, or a cons pair (MAJOR-MODE . CLIENTS),...
Read more >
Flatten nested dictionaries, compressing keys - Stack Overflow
I was searching for a solution that would give a list of the keys on the path to the leaf, not a concatenation....
Read more >
How to flatten list in Python? - TutorialsTeacher
Flatten list of lists means getting elements of sublists into a one dimensional array like list. For example, a list [[1,2,3],[4,5,6]] is ...
Read more >
Fastest Way to Flatten a List in Python - Chris Conlan
Some of the most basic advice people give about optimizing Python code is wrong, likely because no one ever bothered to test it....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found