Skip to content

Add cabal-gild as a cabal file formatter plugin #4101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 42 additions & 37 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
- ubuntu-latest
- macOS-latest
- windows-latest
test:
test:
- true
- false
exclude:
Expand Down Expand Up @@ -112,139 +112,144 @@ jobs:

- if: matrix.test
name: Test hls-graph
run: cabal test hls-graph
run: cabal test hls-graph

- if: needs.pre_job.outputs.should_skip_ghcide != 'true' && matrix.test
name: Test ghcide
# run the tests without parallelism to avoid running out of memory
run: cabal test ghcide || cabal test ghcide
run: cabal test ghcide || cabal test ghcide

- if: matrix.test
name: Test hls-plugin-api
run: cabal test hls-plugin-api || cabal test hls-plugin-api
run: cabal test hls-plugin-api || cabal test hls-plugin-api

- if: matrix.test
name: Test func-test suite
env:
HLS_TEST_EXE: hls
HLS_WRAPPER_TEST_EXE: hls-wrapper
run: cabal test func-test || cabal test func-test
run: cabal test func-test || cabal test func-test

- if: matrix.test
name: Test wrapper-test suite
env:
HLS_TEST_EXE: hls
HLS_WRAPPER_TEST_EXE: hls-wrapper
run: cabal test wrapper-test
run: cabal test wrapper-test

- if: matrix.test
name: Test hls-refactor-plugin
run: cabal test hls-refactor-plugin-tests || cabal test hls-refactor-plugin-tests
run: cabal test hls-refactor-plugin-tests || cabal test hls-refactor-plugin-tests

- if: matrix.test
- if: matrix.test
name: Test hls-floskell-plugin
run: cabal test hls-floskell-plugin-tests || cabal test hls-floskell-plugin-tests
run: cabal test hls-floskell-plugin-tests || cabal test hls-floskell-plugin-tests

- if: matrix.test
name: Test hls-class-plugin
run: cabal test hls-class-plugin-tests || cabal test hls-class-plugin-tests
run: cabal test hls-class-plugin-tests || cabal test hls-class-plugin-tests

- if: matrix.test
name: Test hls-pragmas-plugin
run: cabal test hls-pragmas-plugin-tests || cabal test hls-pragmas-plugin-tests
run: cabal test hls-pragmas-plugin-tests || cabal test hls-pragmas-plugin-tests

- if: matrix.test
name: Test hls-eval-plugin
run: cabal test hls-eval-plugin-tests || cabal test hls-eval-plugin-tests
run: cabal test hls-eval-plugin-tests || cabal test hls-eval-plugin-tests

- if: matrix.test
name: Test hls-splice-plugin
run: cabal test hls-splice-plugin-tests || cabal test hls-splice-plugin-tests
run: cabal test hls-splice-plugin-tests || cabal test hls-splice-plugin-tests

- if: matrix.test && matrix.ghc != '9.2'
name: Test hls-stan-plugin
run: cabal test hls-stan-plugin-tests || cabal test hls-stan-plugin-tests
run: cabal test hls-stan-plugin-tests || cabal test hls-stan-plugin-tests

- if: matrix.test
name: Test hls-stylish-haskell-plugin
run: cabal test hls-stylish-haskell-plugin-tests || cabal test hls-stylish-haskell-plugin-tests
run: cabal test hls-stylish-haskell-plugin-tests || cabal test hls-stylish-haskell-plugin-tests

- if: matrix.test
- if: matrix.test
name: Test hls-ormolu-plugin
run: cabal test hls-ormolu-plugin-tests || cabal test hls-ormolu-plugin-tests
run: cabal test hls-ormolu-plugin-tests || cabal test hls-ormolu-plugin-tests

- if: matrix.test
- if: matrix.test
name: Test hls-fourmolu-plugin
run: cabal test hls-fourmolu-plugin-tests || cabal test hls-fourmolu-plugin-tests
run: cabal test hls-fourmolu-plugin-tests || cabal test hls-fourmolu-plugin-tests

- if: matrix.test
name: Test hls-explicit-imports-plugin test suite
run: cabal test hls-explicit-imports-plugin-tests || cabal test hls-explicit-imports-plugin-tests
run: cabal test hls-explicit-imports-plugin-tests || cabal test hls-explicit-imports-plugin-tests

- if: matrix.test
name: Test hls-call-hierarchy-plugin test suite
run: cabal test hls-call-hierarchy-plugin-tests || cabal test hls-call-hierarchy-plugin-tests
run: cabal test hls-call-hierarchy-plugin-tests || cabal test hls-call-hierarchy-plugin-tests

- if: matrix.test && matrix.os != 'windows-latest'
name: Test hls-rename-plugin test suite
run: cabal test hls-rename-plugin-tests || cabal test hls-rename-plugin-tests
run: cabal test hls-rename-plugin-tests || cabal test hls-rename-plugin-tests

- if: matrix.test
- if: matrix.test
name: Test hls-hlint-plugin test suite
run: cabal test hls-hlint-plugin-tests || cabal test hls-hlint-plugin-tests
run: cabal test hls-hlint-plugin-tests || cabal test hls-hlint-plugin-tests

- if: matrix.test
name: Test hls-module-name-plugin test suite
run: cabal test hls-module-name-plugin-tests || cabal test hls-module-name-plugin-tests
run: cabal test hls-module-name-plugin-tests || cabal test hls-module-name-plugin-tests

- if: matrix.test
name: Test hls-alternate-number-format-plugin test suite
run: cabal test hls-alternate-number-format-plugin-tests || cabal test hls-alternate-number-format-plugin-tests
run: cabal test hls-alternate-number-format-plugin-tests || cabal test hls-alternate-number-format-plugin-tests

- if: matrix.test
name: Test hls-qualify-imported-names-plugin test suite
run: cabal test hls-qualify-imported-names-plugin-tests || cabal test hls-qualify-imported-names-plugin-tests
run: cabal test hls-qualify-imported-names-plugin-tests || cabal test hls-qualify-imported-names-plugin-tests

- if: matrix.test
name: Test hls-code-range-plugin test suite
run: cabal test hls-code-range-plugin-tests || cabal test hls-code-range-plugin-tests
run: cabal test hls-code-range-plugin-tests || cabal test hls-code-range-plugin-tests

- if: matrix.test
name: Test hls-change-type-signature test suite
run: cabal test hls-change-type-signature-plugin-tests || cabal test hls-change-type-signature-plugin-tests
run: cabal test hls-change-type-signature-plugin-tests || cabal test hls-change-type-signature-plugin-tests

- if: matrix.test
name: Test hls-gadt-plugin test suit
run: cabal test hls-gadt-plugin-tests || cabal test hls-gadt-plugin-tests
run: cabal test hls-gadt-plugin-tests || cabal test hls-gadt-plugin-tests

- if: matrix.test
name: Test hls-explicit-fixity-plugin test suite
run: cabal test hls-explicit-fixity-plugin-tests || cabal test hls-explicit-fixity-plugin-tests
run: cabal test hls-explicit-fixity-plugin-tests || cabal test hls-explicit-fixity-plugin-tests

- if: matrix.test
name: Test hls-explicit-record-fields-plugin test suite
run: cabal test hls-explicit-record-fields-plugin-tests || cabal test hls-explicit-record-fields-plugin-tests
run: cabal test hls-explicit-record-fields-plugin-tests || cabal test hls-explicit-record-fields-plugin-tests

## version needs to be limited since the tests depend on cabal-fmt which only builds using specific ghc versions
- if: matrix.test && matrix.ghc == '9.2'
name: Test hls-cabal-fmt-plugin test suite
run: cabal test hls-cabal-fmt-plugin-tests --flag=isolateCabalfmtTests || cabal test hls-cabal-fmt-plugin-tests --flag=isolateCabalfmtTests
run: cabal test hls-cabal-fmt-plugin-tests --flag=isolateCabalfmtTests || cabal test hls-cabal-fmt-plugin-tests --flag=isolateCabalfmtTests

## version needs to be limited since the tests depend on cabal-gild which only builds using specific ghc versions
- if: matrix.test
name: Test hls-cabal-gild-plugin test suite
run: cabal test hls-cabal-gild-plugin-tests --flag=isolateCabalGildTests || cabal test hls-cabal-gild-plugin-tests --flag=isolateCabalGildTests

- if: matrix.test
name: Test hls-cabal-plugin test suite
run: cabal test hls-cabal-plugin-tests || cabal test hls-cabal-plugin-tests
run: cabal test hls-cabal-plugin-tests || cabal test hls-cabal-plugin-tests

- if: matrix.test
name: Test hls-retrie-plugin test suite
run: cabal test hls-retrie-plugin-tests || cabal test hls-retrie-plugin-tests
run: cabal test hls-retrie-plugin-tests || cabal test hls-retrie-plugin-tests

- if: matrix.test
name: Test hls-overloaded-record-dot-plugin test suite
run: cabal test hls-overloaded-record-dot-plugin-tests || cabal test hls-overloaded-record-dot-plugin-tests
run: cabal test hls-overloaded-record-dot-plugin-tests || cabal test hls-overloaded-record-dot-plugin-tests

- if: matrix.test
name: Test hls-semantic-tokens-plugin test suite
run: cabal test hls-semantic-tokens-plugin-tests || cabal test hls-semantic-tokens-plugin-tests
run: cabal test hls-semantic-tokens-plugin-tests || cabal test hls-semantic-tokens-plugin-tests


test_post_job:
Expand Down
7 changes: 4 additions & 3 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
# Plugins
/plugins/hls-alternate-number-format-plugin @drsooch
/plugins/hls-cabal-fmt-plugin @VeryMilkyJoe @fendor
/plugins/hls-cabal-gild-plugin @fendor
/plugins/hls-cabal-plugin @fendor
/plugins/hls-call-hierarchy-plugin @July541
/plugins/hls-change-type-signature-plugin
/plugins/hls-class-plugin
/plugins/hls-class-plugin
/plugins/hls-code-range-plugin @kokobd
/plugins/hls-eval-plugin
/plugins/hls-explicit-fixity-plugin
Expand All @@ -33,7 +34,7 @@
/plugins/hls-pragmas-plugin @eddiemundo
/plugins/hls-qualify-imported-names-plugin @eddiemundo
/plugins/hls-refactor-plugin @santiweight
/plugins/hls-rename-plugin
/plugins/hls-rename-plugin
/plugins/hls-retrie-plugin @pepeiborra
/plugins/hls-semantic-tokens-plugin @soulomoon
/plugins/hls-splice-plugin @konn
Expand All @@ -48,7 +49,7 @@
/docs @michaelpj

# CI
/.circleci
/.circleci
/.github @michaelpj @fendor

# Build
Expand Down
51 changes: 51 additions & 0 deletions haskell-language-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,56 @@ test-suite hls-cabal-fmt-plugin-tests
if flag(isolateCabalfmtTests)
build-tool-depends: cabal-fmt:cabal-fmt ^>=0.1.6

-----------------------------
-- cabal-gild plugin
-----------------------------

flag cabalgild
description: Enable cabal-gild plugin
default: True
manual: True

common cabalgild
if flag(cabalgild)
build-depends: haskell-language-server:hls-cabal-gild-plugin
cpp-options: -Dhls_cabalgild

flag isolateCabalGildTests
description: Should tests search for 'cabal-gild' on the $PATH or shall we install it via build-tool-depends?
-- By default, search on the PATH
default: False
manual: True

library hls-cabal-gild-plugin
import: defaults, pedantic, warnings
exposed-modules: Ide.Plugin.CabalGild
hs-source-dirs: plugins/hls-cabal-gild-plugin/src
build-depends:
, base >=4.12 && <5
, directory
, filepath
, ghcide == 2.7.0.0
, hls-plugin-api == 2.7.0.0
, lsp-types
, text
, mtl
, process-extras

test-suite hls-cabal-gild-plugin-tests
import: defaults, pedantic, test-defaults, warnings
type: exitcode-stdio-1.0
hs-source-dirs: plugins/hls-cabal-gild-plugin/test
main-is: Main.hs
build-depends:
, base
, directory
, filepath
, haskell-language-server:hls-cabal-gild-plugin
, hls-test-utils == 2.7.0.0

if flag(isolateCabalGildTests)
build-tool-depends: cabal-gild:cabal-gild ^>=1.1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do it like cabal-fmt? Or can we do it more like the other formatters, where we use the library by default and have an option to use an executable optionally? I thought we did it like this because cabal-fmt was particularly difficult to build? But maybe cabal-gild is easier?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cabal-gild is easier, but it doesn't compile with ghc 9.2. We can disable the plugin for that GHC version, I don't feel strongly.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would be terribly hard to add support for GHC 9.2. Let me see what I can do.

Aside from that, Gild doesn't really expose a high-level "format" function. That's because I primarily intended for it to be an application rather than a library. The mainWith function can be used that way, but it's not very convenient. I could add a format function. What should the type signature be? Something like ByteString -> IO ByteString?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A format function would be nice, but we need to be able to pass the FilePath option for cabal-gild: discover feature to work.
Perhaps a function Opts -> ByteString -> IO ByteString?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because I primarily intended for it to be an application rather than a library.

That's a good reason for us to interact with it that way.

It can be nice to at least have the option to use a compiled-in library version instead of shelling out to a process, but that does lead to the dread #411

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added support for GHC 9.2 in Gild version 1.1.1.0: https://github.com/tfausak/cabal-gild/releases/tag/1.1.1.0

I'll noodle on a high level formatting function. The existing mainWith function can be used in a pure manner, it's just kind of annoying. See the test suite for an example: https://github.com/tfausak/cabal-gild/blob/747550fb20c4ab4003a199ebacd07130ce16796c/source/test-suite/Main.hs#L999


-----------------------------
-- cabal plugin
-----------------------------
Expand Down Expand Up @@ -1642,6 +1692,7 @@ library
, cabal
, callHierarchy
, cabalfmt
, cabalgild
, changeTypeSignature
, class
, eval
Expand Down
3 changes: 2 additions & 1 deletion hls-plugin-api/src/Ide/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ instance Default Config where
, formattingProvider = "ormolu"
-- , formattingProvider = "floskell"
-- , formattingProvider = "stylish-haskell"
, cabalFormattingProvider = "cabal-fmt"
, cabalFormattingProvider = "gild"
-- , cabalFormattingProvider = "cabal-fmt"
-- this string value needs to kept in sync with the value provided in HlsPlugins
, maxCompletions = 40
, plugins = mempty
Expand Down
78 changes: 78 additions & 0 deletions plugins/hls-cabal-gild-plugin/src/Ide/Plugin/CabalGild.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Ide.Plugin.CabalGild where

import Control.Monad.Except (throwError)
import Control.Monad.IO.Class
import qualified Data.Text as T
import Development.IDE hiding (pluginHandlers)
import Ide.Plugin.Error (PluginError (PluginInternalError, PluginInvalidParams))
import Ide.PluginUtils
import Ide.Types
import Language.LSP.Protocol.Types
import Prelude hiding (log)
import System.Directory
import System.Exit
import System.FilePath
import System.Process.ListLike
import qualified System.Process.Text as Process

data Log
= LogProcessInvocationFailure Int T.Text
| LogReadCreateProcessInfo [String]
| LogInvalidInvocationInfo
| LogFormatterBinNotFound
deriving (Show)

instance Pretty Log where
pretty = \case
LogProcessInvocationFailure exitCode err ->
vcat
[ "Invocation of cabal-gild failed with code" <+> pretty exitCode
, "Stderr:" <+> pretty err
]
LogReadCreateProcessInfo args ->
"Formatter invocation: cabal-gild " <+> pretty args
LogInvalidInvocationInfo -> "Invocation of cabal-gild with range was called but is not supported."
LogFormatterBinNotFound -> "Couldn't find executable 'cabal-gild'"

descriptor :: Recorder (WithPriority Log) -> PluginId -> PluginDescriptor IdeState
descriptor recorder plId =
(defaultCabalPluginDescriptor plId "Provides formatting of cabal files with cabal-gild")
{ pluginHandlers = mkFormattingHandlers (provider recorder)
}

-- | Formatter provider of cabal gild.
-- Formats the given source in either a given Range or the whole Document.
-- If the provider fails an error is returned that can be displayed to the user.
provider :: Recorder (WithPriority Log) -> FormattingHandler IdeState
provider recorder _ _ (FormatRange _) _ _ _ = do
logWith recorder Info LogInvalidInvocationInfo
throwError $ PluginInvalidParams "You cannot format a text-range using cabal-gild."
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You kind of can, depending on what exactly "range" means. For example:

$ echo 'build-depends:{base>=4.19,bytestring>=0.12}' | cabal-gild
build-depends:
  base >=4.19,
  bytestring >=0.12

But I wonder if anyone would actually want this behavior anyway.

Copy link
Collaborator Author

@fendor fendor Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think people could find range formatting useful. But supporting only top-level stanzas and fields... Not sure it's the most intuitive behaviour.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we make an issue so we can put a link to the issue in the error? 😈

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to add support for formatting ranges if someone can point me to a resource that explains how it should work! As shown, Gild can format any subsection of a document that's still syntactically valid. But it can't, for example, format any arbitrary range. Like if you tried to format just the contents of the build-depends field, this is what you'd get:

$ echo 'base>=4.19,bytestring>=0.12' | cabal-gild
base >= 4.19 , bytestring >= 0.12

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to add support for formatting ranges if someone can point me to a resource that explains how it should work!

You mean "The document range formatting request is sent from the client to the server to format a given range in a document" isn't good enough for you? 😂

As shown, Gild can format any subsection of a document that's still syntactically valid. But it can't, for example, format any arbitrary range.

Does it take into account the surrounding context? Perhaps it doesn't matter, but an easy way this kind of formatting could go wrong is if you format the range in isolation and therefore treat it as if it is at the top-level of the structure, which it might not be.

But otherwise I think it's fine if range formatting just doesn't do anything unless you've selected a range that includes a whole element that can be sensibly formatted.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only way it could take surrounding context into account is to maintain leading blank spaces. Otherwise it has no way of knowing that it's "inside" a library section, for example. However that doesn't matter too much for Gild; it doesn't keep track of that context anyway. The field names are unique enough that it just looks for build-depends rather than build-depends within library. See the documentation here: https://hackage.haskell.org/package/cabal-gild-1.1.0.0/docs/CabalGild-Action-FormatFields.html#v:parsers

How does range formatting work exactly? If Gild would only get the range's content as input, then there's not much it can do. But if it gets the entire buffer's input along with a span identifying the range to format, then potentially it could do something nice. (Although that might be a lot of work for me.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if it gets the entire buffer's input along with a span identifying the range to format,

That's what you get, indeed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created an issue for this: tfausak/cabal-gild#42. No idea if or when I'll get around to implementing it though.

provider recorder _ide _ FormatText contents nfp _ = do
let cabalGildArgs = ["--stdin=" <> fp, "--input=-"] -- < Read from stdin
x <- liftIO $ findExecutable "cabal-gild"
case x of
Just _ -> do
log Debug $ LogReadCreateProcessInfo cabalGildArgs
(exitCode, out, err) <-
liftIO $ Process.readCreateProcessWithExitCode
( proc "cabal-gild" cabalGildArgs
)
{ cwd = Just $ takeDirectory fp
}
contents
case exitCode of
ExitFailure code -> do
log Error $ LogProcessInvocationFailure code err
throwError (PluginInternalError "Failed to invoke cabal-gild")
ExitSuccess -> do
let fmtDiff = makeDiffTextEdit contents out
pure $ InL fmtDiff
Nothing -> do
log Error LogFormatterBinNotFound
throwError (PluginInternalError "No installation of cabal-gild could be found. Please install it into your global environment.")
where
fp = fromNormalizedFilePath nfp
log = logWith recorder
Loading