python-language-server icon indicating copy to clipboard operation
python-language-server copied to clipboard

Introduce hierarchical documentSymbol support

Open CXuesong opened this issue 6 years ago • 2 comments

c.f. #407

This PR should make textDocument/documentSymbol reponse use the new DocumentSymbol contract. This means you can have a list of children for each parent symbols. Now the documentSymbol is something like this

{
    "jsonrpc": "2.0",
    "id": "2",
    "result": [
        {
            "name": "ReadFields",
            "detail": "ReadFields",
            "range": {
                "start": {
                    "line": 3,
                    "character": 0
                },
                "end": {
                    "line": 19,
                    "character": 0
                }
            },
            "selectionRange": {
                "start": {
                    "line": 3,
                    "character": 4
                },
                "end": {
                    "line": 3,
                    "character": 14
                }
            },
            "kind": 12,
            "children": [
                {
                    "name": "closeFile",
                    "detail": "ReadFields.closeFile",
                    "range": {
                        "start": {
                            "line": 7,
                            "character": 4
                        },
                        "end": {
                            "line": 7,
                            "character": 21
                        }
                    },
                    "selectionRange": {
                        "start": {
                            "line": 7,
                            "character": 4
                        },
                        "end": {
                            "line": 7,
                            "character": 13
                        }
                    },
                    "kind": 13,
                    "children": null
                },
                {
                    "name": "f",
                    "detail": "ReadFields.f",
                    "range": {
                        "start": {
                            "line": 9,
                            "character": 8
                        },
                        "end": {
                            "line": 9,
                            "character": 22
                        }
                    },
                    "selectionRange": {
                        "start": {
                            "line": 9,
                            "character": 8
                        },
                        "end": {
                            "line": 9,
                            "character": 9
                        }
                    },
                    "kind": 13,
                    "children": null
                },
                {
                    "name": "closeFile",
                    "detail": "ReadFields.closeFile",
                    "range": {
                        "start": {
                            "line": 10,
                            "character": 8
                        },
                        "end": {
                            "line": 10,
                            "character": 24
                        }
                    },
                    "selectionRange": {
                        "start": {
                            "line": 10,
                            "character": 8
                        },
                        "end": {
                            "line": 10,
                            "character": 17
                        }
                    },
                    "kind": 13,
                    "children": null
                },
                {
                    "name": "l",
                    "detail": "ReadFields.l",
                    "range": {
                        "start": {
                            "line": 14,
                            "character": 8
                        },
                        "end": {
                            "line": 17,
                            "character": 0
                        }
                    },
                    "selectionRange": {
                        "start": {
                            "line": 14,
                            "character": 12
                        },
                        "end": {
                            "line": 14,
                            "character": 13
                        }
                    },
                    "kind": 13,
                    "children": null
                },
...

Python source code for reference

import io
from os import path

def ReadFields(f) :
    '''
    f : io.IOBase or str
    '''
    closeFile = False
    if isinstance(f, str) : 
        f = io.open(f)
        closeFile = True
    elif isinstance(f, io.IOBase) : pass
    else : raise TypeError
    try :
        for l in f :
            l = str(l).strip()
            if len(l) > 0 : yield l.split()
    finally :
        if closeFile : f.close()
...

Unfortunately, I haven't tested it thoroughly in VSCode because I simply couldn't start Extension Development Host with yarn run vscode.

CXuesong avatar Apr 07 '19 12:04 CXuesong

Thanks for your interest in palantir/python-language-server, @CXuesong! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

palantirtech avatar Apr 07 '19 12:04 palantirtech

Has this ever been resolved? It would be a great addition.

xiaoxiae avatar Mar 15 '21 22:03 xiaoxiae