CodeWithSwastik / vscode.py

Create VSCode Extensions with python
https://vscodepy.readthedocs.io/
MIT License
199 stars 31 forks source link

How to get working directory in quick pick? #31

Closed joonas-yoon closed 3 years ago

joonas-yoon commented 3 years ago

Hi, I just started with this and thanks for sharing.

I am trying to implement word completion in quick pick with items which files in working directory.

thus I tried as like this:

def search():
    cwd = os.getcwd()     # here, this line has the problem
    file_list = get_files_under(cwd)
    data = list(map(lambda path: vscode.ext.QuickPickItem(path), file_list))
    options = vscode.ext.QuickPickOptions(title='Select file to ...')
    selected = vscode.window.show_quick_pick(data, options)
    if not selected: return
    return selected

As I expected, cwd = '<project root directory as workspace>' like ~/vscode-ext-example/ but actually it is cwd = C:\Users\joonasyoon\AppData\Local\Programs\Microsoft VS Code

how can I get current directory for that?

joonas-yoon commented 3 years ago

Oh, I found what it called. it is showWorkspaceFolderPick.

And as far as I can see in this repo, there is no object/class for vscode.workspace. I think it is related to my issue.

CodeWithSwastik commented 3 years ago

Yes this hasn't been implemented yet, I'll add this to the todo. Thanks for pointing this out.

As a work around, I think you can use TextDocument.uri to get the path of the current TextDocument and you can parse the workspace directory from it.

editor = vscode.window.ActiveTextEditor()
uri = editor.document.uri
joonas-yoon commented 3 years ago

Yes this hasn't been implemented yet, I'll add this to the todo. Thanks for pointing this out.

As a work around, I think you can use TextDocument.uri to get the path of the current TextDocument and you can parse the workspace directory from it.

editor = vscode.window.ActiveTextEditor()
uri = editor.document.uri

Thanks for your reply, but it is not working.

An Error occurred in the python script: Traceback (most recent call last):
  File "~\vscode-ext-test\build\extension.py", line 207, in <module>
    ipc_main()
  File "~\vscode-ext-test\build\extension.py", line 205, in ipc_main
    globals()[sys.argv[1]]()
  File "~\vscode-ext-test\build\extension.py", line 195, in sort_file
    filename = search()
  File "~\vscode-ext-test\build\extension.py", line 171, in search
    cwd = editor.document.uri
AttributeError: 'ActiveTextEditor' object has no attribute 'document'

What should I do? I have no idea why document in it is not setting up