wasp/waspc/test/Analyzer/Parser/SourceSpanTest.hs
Craig McIlwrath 76d9fc4213
[waspls] Add code actions for scaffolding external code (#1316)
When an external import tries to import a symbol from a TypeScript/JavaScript file, waspls now offers quickfix code actions to scaffold a function in that file.

It uses the surrounding context of the external import to determine what code to write for the code action. See [`ScaffoldTsSymbol.hs`](457911d5e9/waspc/waspls/src/Wasp/LSP/Commands/ScaffoldTsSymbol.hs) for a detailed description of how it works. At a high level, there is a `templateForFile` function in `Wasp.LSP.Commands.ScaffoldTsSymbol` that selects the correct template from `data/lsp/templates/ts`. For example, `action.fn.ts` contains a template for scaffolding an `action` function in a TypeScript file and would be used when a code action is requested with the cursor at the location marked by `|`:

```wasp
action createTask {
  fn: import { createTask } from "@server/actions.js"|
}
```

The scaffold action runs as a [LSP command](https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#workspace_executeCommand). To prepare for wanting to define more commands in waspls in the future, this PR also introduces the concept of `Commands` (`Wasp.LSP.Commands.Command`) that define some properties about each command waspls wants to handle.
2023-07-26 08:00:16 -04:00

28 lines
1.2 KiB
Haskell

module Analyzer.Parser.SourceSpanTest where
import Test.QuickCheck
import Test.Tasty.Hspec
import Wasp.Analyzer.Parser.SourceSpan (SourceSpan (SourceSpan), spansOverlap)
spec_SourceSpanTest :: Spec
spec_SourceSpanTest = do
describe "Analyzer.Parser.SourceSpan" $ do
describe "spansOverlap works" $ do
it "when first is before second" $ do
spansOverlap (SourceSpan 0 5) (SourceSpan 10 15) `shouldBe` False
it "when first ends right before second starts" $ do
spansOverlap (SourceSpan 0 5) (SourceSpan 5 10) `shouldBe` False
it "when first overlaps second on its left edge" $ do
spansOverlap (SourceSpan 0 5) (SourceSpan 4 10) `shouldBe` True
it "when first is second" $ do
spansOverlap (SourceSpan 0 5) (SourceSpan 0 5) `shouldBe` True
it "when first overlaps second on its right edge" $ do
spansOverlap (SourceSpan 4 10) (SourceSpan 0 5) `shouldBe` True
it "when second is zero-width" $ do
spansOverlap (SourceSpan 0 5) (SourceSpan 2 2) `shouldBe` True
it "is commutative" $ do
property $ \s0 e0 s1 e1 ->
let first = SourceSpan s0 e0
second = SourceSpan s1 e1
in spansOverlap first second == spansOverlap second first