openlawlibrary / pygls

A pythonic generic language server
https://pygls.readthedocs.io/en/latest/
Apache License 2.0
588 stars 105 forks source link

Fix construction of `workspace.fileOperations` fields in server capabilities #278

Closed alcarney closed 1 year ago

alcarney commented 2 years ago

The server capabilities for each of the the didXXXfiles and willXXXFiles notifications are currently constructed as follows

did_delete = (
    get_capability(self.client_capabilities, 'workspace.fileOperations.didDelete')
    if WORKSPACE_DID_DELETE_FILES in self.features
    else None
)
if did_delete is not None:
    file_operations.did_delete = did_delete

which results in did_delete taking the value of True or None.

Instead it should be an instance of FileOperationRegistrationOptions which allows the server to declare which files it is interested in.

I have a (partial) fix in esbonio for just the did delete files notification.

if WORKSPACE_DID_DELETE_FILES in self.fm.features:
    opts = self.fm.feature_options.get(WORKSPACE_DID_DELETE_FILES, None)
    if opts:
        value.workspace.file_operations.did_delete = opts  # type: ignore

Which allows the following usage pattern

@server.feature(
    WORKSPACE_DID_DELETE_FILES,
    FileOperationRegistrationOptions(
        filters=[
            FileOperationFilter(
                pattern=FileOperationPattern(glob="**/*.rst"),
            )
        ]
    ),
)
def on_delete_files(ls: RstLanguageServer, params: DeleteFilesParams):
    ...

I've been meaning to update it for all the file operation notifications and upstream it for a while but just haven't got around to it - so if someone else feels they have time to pick this up then go for it! :smile: