BetterThanTomorrow / calva

Clojure & ClojureScript Interactive Programming for VS Code
https://marketplace.visualstudio.com/items?itemName=betterthantomorrow.calva
Other
1.67k stars 217 forks source link

"Resolve macros as" sometimes does not work #1788

Closed bpringe closed 2 years ago

bpringe commented 2 years ago

Sometimes a message is shown that no macro exists at the cursor.

Given a file with the following content, and given that the command is run on the second deftest, the logs below are from an unsuccessful and successful run:

Unsuccessful:

[Trace - 9:00:08 AM] Sending request 'workspace/executeCommand - (99)'.
Params: {
    "command": "resolve-macro-as",
    "arguments": [
        "file:///Users/brandon/development/clojure-test/test/core_test.clj",
        7,
        7
    ]
}

[Trace - 9:00:08 AM] Received response 'workspace/executeCommand - (99)' in 1ms.
No result returned.

[Trace - 9:00:08 AM] Received request 'window/showMessageRequest - (18)'.
Params: {
    "actions": [
        {
            "title": "clojure.core/def"
        },
        {
            "title": "clojure.core/defn"
        },
        {
            "title": "clojure.core/let"
        },
        {
            "title": "clojure.core/for"
        },
        {
            "title": "clojure.core/->"
        },
        {
            "title": "clojure.core/->>"
        },
        {
            "title": "clj-kondo.lint-as/def-catch-all"
        }
    ],
    "type": 3,
    "message": "Select how LSP should resolve this macro:"
}

[Trace - 9:00:10 AM] Sending response 'window/showMessageRequest - (18)'. Processing request took 2199ms
Result: {
    "title": "clojure.core/defn"
}

[Trace - 9:00:10 AM] Received request 'window/showMessageRequest - (19)'.
Params: {
    "actions": [
        {
            "title": "/Users/brandon/development/clojure-test/.clj-kondo/config.edn"
        },
        {
            "title": "/Users/brandon/.config/clj-kondo/config.edn"
        }
    ],
    "type": 3,
    "message": "Select where LSP should save this setting:"
}

[Trace - 9:00:11 AM] Sending response 'window/showMessageRequest - (19)'. Processing request took 1258ms
Result: {
    "title": "/Users/brandon/development/clojure-test/.clj-kondo/config.edn"
}

[Trace - 9:00:11 AM] Received notification 'window/showMessage'.
Params: {
    "type": 1,
    "message": "No macro was found at cursor to resolve as 'clojure.core/defn'."
}

Successful:

[Trace - 9:06:23 AM] Sending request 'workspace/executeCommand - (155)'.
Params: {
    "command": "resolve-macro-as",
    "arguments": [
        "file:///Users/brandon/development/clojure-test/test/core_test.clj",
        7,
        5
    ]
}

[Trace - 9:06:23 AM] Received response 'workspace/executeCommand - (155)' in 1ms.
No result returned.

[Trace - 9:06:23 AM] Received request 'window/showMessageRequest - (50)'.
Params: {
    "actions": [
        {
            "title": "clojure.core/def"
        },
        {
            "title": "clojure.core/defn"
        },
        {
            "title": "clojure.core/let"
        },
        {
            "title": "clojure.core/for"
        },
        {
            "title": "clojure.core/->"
        },
        {
            "title": "clojure.core/->>"
        },
        {
            "title": "clj-kondo.lint-as/def-catch-all"
        }
    ],
    "type": 3,
    "message": "Select how LSP should resolve this macro:"
}

[Trace - 9:06:25 AM] Sending response 'window/showMessageRequest - (50)'. Processing request took 1498ms
Result: {
    "title": "clojure.core/defn"
}

[Trace - 9:06:25 AM] Received request 'window/showMessageRequest - (51)'.
Params: {
    "actions": [
        {
            "title": "/Users/brandon/development/clojure-test/.clj-kondo/config.edn"
        },
        {
            "title": "/Users/brandon/.config/clj-kondo/config.edn"
        }
    ],
    "type": 3,
    "message": "Select where LSP should save this setting:"
}

[Trace - 9:06:26 AM] Sending response 'window/showMessageRequest - (51)'. Processing request took 793ms
Result: {
    "title": "/Users/brandon/development/clojure-test/.clj-kondo/config.edn"
}

I noticed that if I put the cursor after the t in deftest the command always fails, but if I put it before the t, the command always succeeds.

@ericdallo Any idea what's going on here?

PEZ commented 2 years ago

I noticed that if I put the cursor after the t in deftest the command always fails, but if I put it before the t, the command always succeeds.

Perhaps if instead of sending the active selection, we send the selection of Calva's current form to this command, then it would better meet the expectations of Calva users?

https://github.com/BetterThanTomorrow/calva/blob/dev/src/lsp/main.ts#L304

ericdallo commented 2 years ago

I think we should fix on clojure-lsp to be a little bit smart like other refactorings to parse current cursor and know if it's in a macro and get it's symbol properly

bpringe commented 2 years ago

This was fixed in clojure-lsp and should be released soon. I'm going to close this so we don't have to remember to close it later when the fix is released.