From af9521491bb629a9a1d8442e09a2db148f5a16ed Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Tue, 15 Dec 2020 14:44:29 +0800 Subject: [PATCH 01/10] Add haddock comments plugin : Update project config files Add CPP Add missing include-dirs Refactor Support records Only suggest when no error Don't suggest if nothing to change Update code action title and comment template Remove toCurrentRange Add haddock comments plugin Update project config files Add CPP Remove temp files Remove temp files Remove spaces --- cabal.project | 1 + exe/Plugins.hs | 7 + haskell-language-server.cabal | 11 + hie-cabal.yaml | 3 + hie-stack.yaml | 3 + nix/default.nix | 1 + plugins/hls-haddock-comments-plugin/LICENSE | 201 ++++++++++++++++++ .../hls-haddock-comments-plugin.cabal | 33 +++ .../include/ghc-api-version.h | 10 + .../src/Ide/Plugin/HaddockComments.hs | 154 ++++++++++++++ shell.nix | 1 + stack-8.10.1.yaml | 1 + stack-8.10.2.yaml | 1 + stack-8.6.4.yaml | 1 + stack-8.6.5.yaml | 1 + stack-8.8.2.yaml | 1 + stack-8.8.3.yaml | 1 + stack-8.8.4.yaml | 1 + stack.yaml | 1 + 19 files changed, 433 insertions(+) create mode 100644 plugins/hls-haddock-comments-plugin/LICENSE create mode 100644 plugins/hls-haddock-comments-plugin/hls-haddock-comments-plugin.cabal create mode 100644 plugins/hls-haddock-comments-plugin/include/ghc-api-version.h create mode 100644 plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs diff --git a/cabal.project b/cabal.project index 51d8cc1aba..8ecc3ac99f 100644 --- a/cabal.project +++ b/cabal.project @@ -10,6 +10,7 @@ packages: ./plugins/hls-explicit-imports-plugin ./plugins/hls-hlint-plugin ./plugins/hls-retrie-plugin + ./plugins/hls-haddock-comments-plugin tests: true diff --git a/exe/Plugins.hs b/exe/Plugins.hs index 1bd2336e9c..a461bb1879 100644 --- a/exe/Plugins.hs +++ b/exe/Plugins.hs @@ -17,6 +17,10 @@ import Development.IDE.Plugin.HLS.GhcIde as GhcIde import Ide.Plugin.Class as Class #endif +#if haddockComments +import Ide.Plugin.HaddockComments as HaddockComments +#endif + #if eval import Ide.Plugin.Eval as Eval #endif @@ -109,6 +113,9 @@ idePlugins includeExamples = pluginDescToIdePlugins allPlugins #if class , Class.descriptor "class" #endif +#if haddockComments + , HaddockComments.descriptor "haddockComments" +#endif #if eval , Eval.descriptor "eval" #endif diff --git a/haskell-language-server.cabal b/haskell-language-server.cabal index ce7166f7da..55be92d62e 100644 --- a/haskell-language-server.cabal +++ b/haskell-language-server.cabal @@ -92,6 +92,11 @@ flag class default: False manual: True +flag haddockComments + description: Enable haddockComments plugin + default: False + manual: True + flag eval description: Enable eval plugin default: False @@ -164,6 +169,11 @@ common class build-depends: hls-class-plugin cpp-options: -Dclass +common haddockComments + if flag(haddockComments) || flag(all-plugins) + build-depends: hls-haddock-comments-plugin + cpp-options: -DhaddockComments + common eval if flag(eval) || flag(all-plugins) build-depends: hls-eval-plugin @@ -244,6 +254,7 @@ executable haskell-language-server -- plugins , example-plugins , class + , haddockComments , eval , importLens , retrie diff --git a/hie-cabal.yaml b/hie-cabal.yaml index 336cbe26b4..9ebe058f52 100644 --- a/hie-cabal.yaml +++ b/hie-cabal.yaml @@ -106,6 +106,9 @@ cradle: - path: "./plugins/hls-class-plugin/src" component: "lib:hls-class-plugin" + - path: "./plugins/hls-haddock-comments-plugin/src" + component: "hls-haddock-comments-plugin" + - path: "./plugins/hls-eval-plugin/src" component: "lib:hls-eval-plugin" diff --git a/hie-stack.yaml b/hie-stack.yaml index 2891e08e8f..31414c9402 100644 --- a/hie-stack.yaml +++ b/hie-stack.yaml @@ -62,3 +62,6 @@ cradle: - path: "./plugins/tactics/test" component: "hls-tactics-plugin:test:tests" + + - path: "./plugins/hls-haddock-comments-plugin/src" + component: "hls-haddock-comments-plugin:lib" diff --git a/nix/default.nix b/nix/default.nix index 17e424f118..6916d96052 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -21,6 +21,7 @@ let hie-compat = gitignoreSource ../hie-compat; hls-plugin-api = gitignoreSource ../hls-plugin-api; hls-class-plugin = gitignoreSource ../plugins/hls-class-plugin; + hls-haddock-comments-plugin = gitignoreSource ../plugins/hls-haddock-comments-plugin; hls-eval-plugin = gitignoreSource ../plugins/hls-eval-plugin; hls-explicit-imports-plugin = gitignoreSource ../plugins/hls-explicit-imports-plugin; hls-hlint-plugin = gitignoreSource ../plugins/hls-hlint-plugin; diff --git a/plugins/hls-haddock-comments-plugin/LICENSE b/plugins/hls-haddock-comments-plugin/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/plugins/hls-haddock-comments-plugin/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/plugins/hls-haddock-comments-plugin/hls-haddock-comments-plugin.cabal b/plugins/hls-haddock-comments-plugin/hls-haddock-comments-plugin.cabal new file mode 100644 index 0000000000..34e997dbdb --- /dev/null +++ b/plugins/hls-haddock-comments-plugin/hls-haddock-comments-plugin.cabal @@ -0,0 +1,33 @@ +cabal-version: 2.2 +name: hls-haddock-comments-plugin +version: 0.1.0.0 +synopsis: + Haddock comments generator plugin for Haskell Language Server + +license: Apache-2.0 +license-file: LICENSE +author: Potato Hatsue +maintainer: berberman@yandex.com +category: Development +build-type: Simple +extra-source-files: + include/ghc-api-version.h + LICENSE + +library + exposed-modules: Ide.Plugin.HaddockComments + hs-source-dirs: src + ghc-options: -Wall -Wno-name-shadowing -Wredundant-constraints + build-depends: + , base + , containers + , ghc + , ghc-exactprint + , ghcide + , haskell-lsp-types + , hls-plugin-api + , text + , unordered-containers + + default-language: Haskell2010 + include-dirs: include diff --git a/plugins/hls-haddock-comments-plugin/include/ghc-api-version.h b/plugins/hls-haddock-comments-plugin/include/ghc-api-version.h new file mode 100644 index 0000000000..11cabb3dc9 --- /dev/null +++ b/plugins/hls-haddock-comments-plugin/include/ghc-api-version.h @@ -0,0 +1,10 @@ +#ifndef GHC_API_VERSION_H +#define GHC_API_VERSION_H + +#ifdef GHC_LIB +#define MIN_GHC_API_VERSION(x,y,z) MIN_VERSION_ghc_lib(x,y,z) +#else +#define MIN_GHC_API_VERSION(x,y,z) MIN_VERSION_ghc(x,y,z) +#endif + +#endif diff --git a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs new file mode 100644 index 0000000000..a9fb0504cb --- /dev/null +++ b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs @@ -0,0 +1,154 @@ +{-# LANGUAGE CPP #-} +{-# LANGUAGE ExistentialQuantification #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE ViewPatterns #-} + +#include "ghc-api-version.h" + +module Ide.Plugin.HaddockComments where + +import Control.Monad (join) +import qualified Data.HashMap.Strict as HashMap +import qualified Data.Map as Map +import qualified Data.Text as T +import Development.IDE +import Development.IDE.GHC.Compat +import Ide.Types +import Language.Haskell.GHC.ExactPrint +import Language.Haskell.GHC.ExactPrint.Types hiding (GhcPs) +import Language.Haskell.GHC.ExactPrint.Utils +import Language.Haskell.LSP.Types + +----------------------------------------------------------------------------- +descriptor :: PluginId -> PluginDescriptor +descriptor plId = + (defaultPluginDescriptor plId) + { pluginCodeActionProvider = Just codeActionProvider + } + +haddockCommentsId :: CommandId +haddockCommentsId = "HaddockCommentsCommand" + +codeActionProvider :: CodeActionProvider +codeActionProvider _lspFuncs ideState _pId (TextDocumentIdentifier uri) range CodeActionContext {_diagnostics = List diags} = + do + let noErr = and $ (/= Just DsError) . _severity <$> diags + nfp = uriToNormalizedFilePath $ toNormalizedUri uri + (join -> pm) <- runAction "HaddockComments.GetParsedModule" ideState $ use GetParsedModule `traverse` nfp + let locDecls = hsmodDecls . unLoc . pm_parsed_source <$> pm + anns = relativiseApiAnns <$> (pm_parsed_source <$> pm) <*> (pm_annotations <$> pm) + edits = [runGenComments gen locDecls anns range | noErr, gen <- genList] + return $ Right $ List [CACodeAction $ toAction title uri edit | (Just (title, edit)) <- edits] + +genList :: [GenComments] +genList = + [ genForSig, + genForRecord + ] + +----------------------------------------------------------------------------- +data GenComments = forall a. + GenComments + { title :: T.Text, + fromDecl :: HsDecl GhcPs -> Maybe a, + collectKeys :: a -> [AnnKey], + isFresh :: Annotation -> Bool, + updateAnn :: Annotation -> Annotation + } + +runGenComments :: GenComments -> Maybe [LHsDecl GhcPs] -> Maybe Anns -> Range -> Maybe (T.Text, TextEdit) +runGenComments GenComments {..} mLocDecls mAnns range + | Just locDecls <- mLocDecls, + Just anns <- mAnns, + [(locDecl, src, x)] <- [(locDecl, l, x) | locDecl@(L l (fromDecl -> Just x)) <- locDecls, inRange range l], + annKeys <- collectKeys x, + not $ null annKeys, + and $ maybe False isFresh . flip Map.lookup anns <$> annKeys, + anns' <- foldr (Map.adjust updateAnn) anns annKeys, + Just range' <- calcRange src range, + result <- T.strip . T.pack $ exactPrint locDecl anns' = + Just (title, TextEdit range' result) + | otherwise = Nothing + +----------------------------------------------------------------------------- + +genForSig :: GenComments +genForSig = GenComments {..} + where + title = "Generate signature comments" + + fromDecl (SigD _ (TypeSig _ _ (HsWC _ (HsIB _ x)))) = Just x + fromDecl _ = Nothing + updateAnn x = x {annEntryDelta = DP (0, 1), annsDP = dp} + + isFresh Ann {annsDP} + | null [() | (AnnComment _, _) <- annsDP] = True + | otherwise = False + + collectKeys = keyFromTyVar 0 + + comment = mkComment "-- ^ " noSrcSpan + dp = [(AnnComment comment, DP (0, 1)), (G AnnRarrow, DP (1, 2))] + +genForRecord :: GenComments +genForRecord = GenComments {..} + where + title = "Generate fields comments" + + fromDecl (TyClD _ DataDecl {tcdDataDefn = HsDataDefn {dd_cons = cons}}) = Just [x | (L _ ConDeclH98 {con_args = x}) <- cons] + fromDecl _ = Nothing + + updateAnn x = x {annEntryDelta = DP (1, -7), annPriorComments = [(comment, DP (1, -7))]} + + isFresh Ann {annPriorComments} = null annPriorComments + + collectKeys = keyFromCon + + comment = mkComment "-- | " noSrcSpan + +----------------------------------------------------------------------------- + +toAction :: T.Text -> Uri -> TextEdit -> CodeAction +toAction title uri edit = CodeAction {..} + where + _title = title + _kind = Just CodeActionQuickFix + _diagnostics = Nothing + _command = Nothing + _changes = Just $ HashMap.singleton uri $ List [edit] + _documentChanges = Nothing + _edit = Just WorkspaceEdit {..} + +calcRange :: SrcSpan -> Range -> Maybe Range +calcRange src range + | inRange range src, + (RealSrcSpan span) <- src, + range' <- realSrcSpanToRange span = + Just range' + | otherwise = Nothing + +inRange :: Range -> SrcSpan -> Bool +inRange range x = isInsideSrcSpan (_start range) x || isInsideSrcSpan (_end range) x + +----------------------------------------------------------------------------- + +keyFromTyVar :: Int -> LHsType GhcPs -> [AnnKey] +keyFromTyVar dep c@(L _ (HsFunTy _ x y)) + | dep < 1 = mkAnnKey c : keyFromTyVar dep x ++ keyFromTyVar dep y + | otherwise = [] +#if MIN_GHC_API_VERSION(8,10,0) +keyFromTyVar dep (L _ (HsForAllTy _ _ _ x)) = keyFromTyVar dep x +#else +keyFromTyVar dep (L _ (HsForAllTy _ _ x)) = keyFromTyVar dep x +#endif +keyFromTyVar dep (L _ (HsQualTy _ _ x)) = keyFromTyVar dep x +keyFromTyVar dep (L _ (HsParTy _ x)) = keyFromTyVar (succ dep) x +keyFromTyVar dep (L _ (HsBangTy _ _ x)) = keyFromTyVar dep x +keyFromTyVar _ _ = [] + +keyFromCon :: [HsConDeclDetails GhcPs] -> [AnnKey] +keyFromCon cons = mconcat [mkAnnKey <$> xs | (RecCon (L _ xs)) <- cons] + +----------------------------------------------------------------------------- diff --git a/shell.nix b/shell.nix index 8c1a0f626a..bb5e8f3215 100644 --- a/shell.nix +++ b/shell.nix @@ -30,6 +30,7 @@ let defaultCompiler = "ghc" + lib.replaceStrings ["."] [""] haskellPackages.ghc. p.hie-compat p.hls-plugin-api p.hls-class-plugin + p.hls-haddock-comments-plugin p.hls-eval-plugin p.hls-explicit-imports-plugin p.hls-hlint-plugin diff --git a/stack-8.10.1.yaml b/stack-8.10.1.yaml index 80d86db779..839e8260c7 100644 --- a/stack-8.10.1.yaml +++ b/stack-8.10.1.yaml @@ -7,6 +7,7 @@ packages: # - ./shake-bench - ./hls-plugin-api - ./plugins/hls-class-plugin +- ./plugins/hls-haddock-comments-plugin - ./plugins/hls-eval-plugin - ./plugins/hls-explicit-imports-plugin - ./plugins/hls-hlint-plugin diff --git a/stack-8.10.2.yaml b/stack-8.10.2.yaml index 6b366dfbdd..6c07a409b9 100644 --- a/stack-8.10.2.yaml +++ b/stack-8.10.2.yaml @@ -7,6 +7,7 @@ packages: - ./hls-plugin-api # - ./shake-bench - ./plugins/hls-class-plugin + - ./plugins/hls-haddock-comments-plugin - ./plugins/hls-eval-plugin - ./plugins/hls-explicit-imports-plugin - ./plugins/hls-hlint-plugin diff --git a/stack-8.6.4.yaml b/stack-8.6.4.yaml index 1cdd6507b8..a5c21e9b72 100644 --- a/stack-8.6.4.yaml +++ b/stack-8.6.4.yaml @@ -8,6 +8,7 @@ packages: # - ./shake-bench - ./hls-plugin-api - ./plugins/hls-class-plugin + - ./plugins/hls-haddock-comments-plugin - ./plugins/hls-eval-plugin - ./plugins/hls-explicit-imports-plugin - ./plugins/hls-hlint-plugin diff --git a/stack-8.6.5.yaml b/stack-8.6.5.yaml index fa805f9276..e560f6942c 100644 --- a/stack-8.6.5.yaml +++ b/stack-8.6.5.yaml @@ -7,6 +7,7 @@ packages: - ./hls-plugin-api # - ./shake-bench - ./plugins/hls-class-plugin + - ./plugins/hls-haddock-comments-plugin - ./plugins/hls-eval-plugin - ./plugins/hls-explicit-imports-plugin - ./plugins/hls-hlint-plugin diff --git a/stack-8.8.2.yaml b/stack-8.8.2.yaml index 6f12ce8aca..a8bb8553aa 100644 --- a/stack-8.8.2.yaml +++ b/stack-8.8.2.yaml @@ -7,6 +7,7 @@ packages: - ./hls-plugin-api - ./shake-bench - ./plugins/hls-class-plugin + - ./plugins/hls-haddock-comments-plugin - ./plugins/hls-eval-plugin - ./plugins/hls-explicit-imports-plugin - ./plugins/hls-hlint-plugin diff --git a/stack-8.8.3.yaml b/stack-8.8.3.yaml index 57d65a386d..5d304b30af 100644 --- a/stack-8.8.3.yaml +++ b/stack-8.8.3.yaml @@ -7,6 +7,7 @@ packages: - ./shake-bench - ./hls-plugin-api - ./plugins/hls-class-plugin + - ./plugins/hls-haddock-comments-plugin - ./plugins/hls-eval-plugin - ./plugins/hls-explicit-imports-plugin - ./plugins/hls-hlint-plugin diff --git a/stack-8.8.4.yaml b/stack-8.8.4.yaml index 2e8f5bdab7..7171407f3a 100644 --- a/stack-8.8.4.yaml +++ b/stack-8.8.4.yaml @@ -7,6 +7,7 @@ packages: - ./shake-bench - ./hls-plugin-api - ./plugins/hls-class-plugin + - ./plugins/hls-haddock-comments-plugin - ./plugins/hls-eval-plugin - ./plugins/hls-explicit-imports-plugin - ./plugins/hls-hlint-plugin diff --git a/stack.yaml b/stack.yaml index fa805f9276..e560f6942c 100644 --- a/stack.yaml +++ b/stack.yaml @@ -7,6 +7,7 @@ packages: - ./hls-plugin-api # - ./shake-bench - ./plugins/hls-class-plugin + - ./plugins/hls-haddock-comments-plugin - ./plugins/hls-eval-plugin - ./plugins/hls-explicit-imports-plugin - ./plugins/hls-hlint-plugin From 771e0ba894a6e0399c44c964793958124e5fcc91 Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Tue, 5 Jan 2021 12:57:20 +0800 Subject: [PATCH 02/10] Minor refactor, remove CPP --- .../hls-haddock-comments-plugin.cabal | 2 -- .../include/ghc-api-version.h | 10 ------ .../src/Ide/Plugin/HaddockComments.hs | 34 ++++++++----------- 3 files changed, 14 insertions(+), 32 deletions(-) delete mode 100644 plugins/hls-haddock-comments-plugin/include/ghc-api-version.h diff --git a/plugins/hls-haddock-comments-plugin/hls-haddock-comments-plugin.cabal b/plugins/hls-haddock-comments-plugin/hls-haddock-comments-plugin.cabal index 34e997dbdb..1ac864aab1 100644 --- a/plugins/hls-haddock-comments-plugin/hls-haddock-comments-plugin.cabal +++ b/plugins/hls-haddock-comments-plugin/hls-haddock-comments-plugin.cabal @@ -11,7 +11,6 @@ maintainer: berberman@yandex.com category: Development build-type: Simple extra-source-files: - include/ghc-api-version.h LICENSE library @@ -30,4 +29,3 @@ library , unordered-containers default-language: Haskell2010 - include-dirs: include diff --git a/plugins/hls-haddock-comments-plugin/include/ghc-api-version.h b/plugins/hls-haddock-comments-plugin/include/ghc-api-version.h deleted file mode 100644 index 11cabb3dc9..0000000000 --- a/plugins/hls-haddock-comments-plugin/include/ghc-api-version.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef GHC_API_VERSION_H -#define GHC_API_VERSION_H - -#ifdef GHC_LIB -#define MIN_GHC_API_VERSION(x,y,z) MIN_VERSION_ghc_lib(x,y,z) -#else -#define MIN_GHC_API_VERSION(x,y,z) MIN_VERSION_ghc(x,y,z) -#endif - -#endif diff --git a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs index a9fb0504cb..aff7c9a06c 100644 --- a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs +++ b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs @@ -1,12 +1,9 @@ -{-# LANGUAGE CPP #-} {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ViewPatterns #-} -#include "ghc-api-version.h" - module Ide.Plugin.HaddockComments where import Control.Monad (join) @@ -22,7 +19,7 @@ import Language.Haskell.GHC.ExactPrint.Utils import Language.Haskell.LSP.Types ----------------------------------------------------------------------------- -descriptor :: PluginId -> PluginDescriptor +descriptor :: PluginId -> PluginDescriptor IdeState descriptor plId = (defaultPluginDescriptor plId) { pluginCodeActionProvider = Just codeActionProvider @@ -31,7 +28,7 @@ descriptor plId = haddockCommentsId :: CommandId haddockCommentsId = "HaddockCommentsCommand" -codeActionProvider :: CodeActionProvider +codeActionProvider :: CodeActionProvider IdeState codeActionProvider _lspFuncs ideState _pId (TextDocumentIdentifier uri) range CodeActionContext {_diagnostics = List diags} = do let noErr = and $ (/= Just DsError) . _severity <$> diags @@ -62,12 +59,12 @@ runGenComments :: GenComments -> Maybe [LHsDecl GhcPs] -> Maybe Anns -> Range -> runGenComments GenComments {..} mLocDecls mAnns range | Just locDecls <- mLocDecls, Just anns <- mAnns, - [(locDecl, src, x)] <- [(locDecl, l, x) | locDecl@(L l (fromDecl -> Just x)) <- locDecls, inRange range l], + [(locDecl, src, x)] <- [(locDecl, l, x) | locDecl@(L l (fromDecl -> Just x)) <- locDecls, range `isIntersectWith` l], annKeys <- collectKeys x, not $ null annKeys, and $ maybe False isFresh . flip Map.lookup anns <$> annKeys, anns' <- foldr (Map.adjust updateAnn) anns annKeys, - Just range' <- calcRange src range, + Just range' <- toRange src, result <- T.strip . T.pack $ exactPrint locDecl anns' = Just (title, TextEdit range' result) | otherwise = Nothing @@ -97,7 +94,8 @@ genForRecord = GenComments {..} where title = "Generate fields comments" - fromDecl (TyClD _ DataDecl {tcdDataDefn = HsDataDefn {dd_cons = cons}}) = Just [x | (L _ ConDeclH98 {con_args = x}) <- cons] + fromDecl (TyClD _ DataDecl {tcdDataDefn = HsDataDefn {dd_cons = cons}}) = + Just [x | (L _ ConDeclH98 {con_args = x}) <- cons] fromDecl _ = Nothing updateAnn x = x {annEntryDelta = DP (1, -7), annPriorComments = [(comment, DP (1, -7))]} @@ -121,16 +119,15 @@ toAction title uri edit = CodeAction {..} _documentChanges = Nothing _edit = Just WorkspaceEdit {..} -calcRange :: SrcSpan -> Range -> Maybe Range -calcRange src range - | inRange range src, - (RealSrcSpan span) <- src, +toRange :: SrcSpan -> Maybe Range +toRange src + | (RealSrcSpan span) <- src, range' <- realSrcSpanToRange span = Just range' | otherwise = Nothing -inRange :: Range -> SrcSpan -> Bool -inRange range x = isInsideSrcSpan (_start range) x || isInsideSrcSpan (_end range) x +isIntersectWith :: Range -> SrcSpan -> Bool +isIntersectWith Range {_start, _end} x = isInsideSrcSpan _start x || isInsideSrcSpan _end x ----------------------------------------------------------------------------- @@ -138,12 +135,9 @@ keyFromTyVar :: Int -> LHsType GhcPs -> [AnnKey] keyFromTyVar dep c@(L _ (HsFunTy _ x y)) | dep < 1 = mkAnnKey c : keyFromTyVar dep x ++ keyFromTyVar dep y | otherwise = [] -#if MIN_GHC_API_VERSION(8,10,0) -keyFromTyVar dep (L _ (HsForAllTy _ _ _ x)) = keyFromTyVar dep x -#else -keyFromTyVar dep (L _ (HsForAllTy _ _ x)) = keyFromTyVar dep x -#endif -keyFromTyVar dep (L _ (HsQualTy _ _ x)) = keyFromTyVar dep x +keyFromTyVar dep (L _ t@HsForAllTy {}) = keyFromTyVar dep (hst_body t) +keyFromTyVar dep (L _ t@HsQualTy {}) = keyFromTyVar dep (hst_body t) +keyFromTyVar dep (L _ (HsKindSig _ x _)) = keyFromTyVar dep x keyFromTyVar dep (L _ (HsParTy _ x)) = keyFromTyVar (succ dep) x keyFromTyVar dep (L _ (HsBangTy _ _ x)) = keyFromTyVar dep x keyFromTyVar _ _ = [] From 7c01e738098663a8ee7d111e6badbfcc546cd6e2 Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Wed, 6 Jan 2021 09:47:15 +0800 Subject: [PATCH 03/10] Fix stack-8.10.3.yaml --- .../src/Ide/Plugin/HaddockComments.hs | 5 ++--- stack-8.10.3.yaml | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs index aff7c9a06c..bb296f5229 100644 --- a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs +++ b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs @@ -78,11 +78,10 @@ genForSig = GenComments {..} fromDecl (SigD _ (TypeSig _ _ (HsWC _ (HsIB _ x)))) = Just x fromDecl _ = Nothing + updateAnn x = x {annEntryDelta = DP (0, 1), annsDP = dp} - isFresh Ann {annsDP} - | null [() | (AnnComment _, _) <- annsDP] = True - | otherwise = False + isFresh Ann {annsDP} = null [() | (AnnComment _, _) <- annsDP] collectKeys = keyFromTyVar 0 diff --git a/stack-8.10.3.yaml b/stack-8.10.3.yaml index 8375d928e8..94b78091fe 100644 --- a/stack-8.10.3.yaml +++ b/stack-8.10.3.yaml @@ -8,6 +8,7 @@ packages: - ./hls-exactprint-utils # - ./shake-bench - ./plugins/hls-class-plugin + - ./plugins/hls-haddock-comments-plugin - ./plugins/hls-eval-plugin - ./plugins/hls-explicit-imports-plugin - ./plugins/hls-hlint-plugin From f7687dc8fad74508d9511415f5e0b62019f70397 Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Wed, 6 Jan 2021 12:09:52 +0800 Subject: [PATCH 04/10] Add test cases (unverified) --- haskell-language-server.cabal | 1 + test/functional/HaddockComments.hs | 69 +++++++++++++++++++ test/functional/Main.hs | 2 + .../testdata/haddockComments/ConstFunction.hs | 4 ++ .../haddockComments/HigherRankFunction.hs | 6 ++ .../HigherRankFunction.hs.expected | 9 +++ .../haddockComments/KindSigFunction.hs | 11 +++ .../KindSigFunction.hs.expected | 12 ++++ .../haddockComments/MultivariateFunction.hs | 4 ++ .../MultivariateFunction.hs.expected | 11 +++ test/testdata/haddockComments/QualFunction.hs | 4 ++ .../haddockComments/QualFunction.hs.expected | 6 ++ test/testdata/haddockComments/Record.hs | 13 ++++ .../haddockComments/Record.hs.expected | 19 +++++ .../testdata/haddockComments/StaleFunction.hs | 6 ++ test/testdata/haddockComments/StaleRecord.hs | 6 ++ 16 files changed, 183 insertions(+) create mode 100644 test/functional/HaddockComments.hs create mode 100644 test/testdata/haddockComments/ConstFunction.hs create mode 100644 test/testdata/haddockComments/HigherRankFunction.hs create mode 100644 test/testdata/haddockComments/HigherRankFunction.hs.expected create mode 100644 test/testdata/haddockComments/KindSigFunction.hs create mode 100644 test/testdata/haddockComments/KindSigFunction.hs.expected create mode 100644 test/testdata/haddockComments/MultivariateFunction.hs create mode 100644 test/testdata/haddockComments/MultivariateFunction.hs.expected create mode 100644 test/testdata/haddockComments/QualFunction.hs create mode 100644 test/testdata/haddockComments/QualFunction.hs.expected create mode 100644 test/testdata/haddockComments/Record.hs create mode 100644 test/testdata/haddockComments/Record.hs.expected create mode 100644 test/testdata/haddockComments/StaleFunction.hs create mode 100644 test/testdata/haddockComments/StaleRecord.hs diff --git a/haskell-language-server.cabal b/haskell-language-server.cabal index a0383ed814..ec3046aac2 100644 --- a/haskell-language-server.cabal +++ b/haskell-language-server.cabal @@ -434,6 +434,7 @@ test-suite func-test TypeDefinition Tactic Splice + HaddockComments Ide.Plugin.Splice.Types Ide.Plugin.Tactic.TestTypes diff --git a/test/functional/HaddockComments.hs b/test/functional/HaddockComments.hs new file mode 100644 index 0000000000..df6c0d3102 --- /dev/null +++ b/test/functional/HaddockComments.hs @@ -0,0 +1,69 @@ +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE ViewPatterns #-} + +module HaddockComments + ( tests, + ) +where + +import Control.Monad.IO.Class (liftIO) +import Data.Foldable (find) +import Data.Maybe (mapMaybe) +import Data.Text (Text) +import qualified Data.Text.IO as T +import Language.Haskell.LSP.Test +import Language.Haskell.LSP.Types +import System.FilePath ((<.>), ()) +import Test.Hls.Util +import Test.Tasty +import Test.Tasty.HUnit + +tests :: TestTree +tests = + testGroup + "haddock comments" + [ normal "HigherRankFunction.hs" Signature 4 6, + normal "KindSigFunction.hs" Signature 9 10, + normal "MultivariateFunction.hs" Signature 2 8, + normal "QualFunction.hs" Signature 2 10, + normal "Record.hs" Record 7 2, + expectedNothing "StaleFunction.hs" Signature 3 3, + expectedNothing "StaleRecord.hs" Record 3 12 + ] + +normal :: FilePath -> GenCommentsType -> Int -> Int -> TestTree +normal fp (toTitle -> expectedTitle) l c = testCase fp $ + runSession hlsCommand fullCaps haddockCommentsPath $ do + doc <- openDoc fp "haskell" + _ <- waitForDiagnostics + actions <- getCodeActions doc (Range (Position l c) (Position l $ succ c)) + case find ((== Just expectedTitle) . caTitle) actions of + Just (CACodeAction x) -> do + executeCodeAction x + contentAfterAction <- documentContents doc + expected <- liftIO . T.readFile $ haddockCommentsPath fp <.> "expected" + liftIO $ contentAfterAction @?= expected + _ -> liftIO $ assertFailure "Unable to find CodeAction" + +expectedNothing :: FilePath -> GenCommentsType -> Int -> Int -> TestTree +expectedNothing fp (toTitle -> expectedTitle) l c = testCase fp $ + runSession hlsCommand fullCaps haddockCommentsPath $ do + doc <- openDoc fp "haskell" + _ <- waitForDiagnostics + titles <- mapMaybe caTitle <$> getCodeActions doc (Range (Position l c) (Position l $ succ c)) + liftIO $ expectedTitle `notElem` titles @? "Unexpected CodeAction" + +data GenCommentsType = Signature | Record + +toTitle :: GenCommentsType -> Text +toTitle Signature = "Generate signature comments" +toTitle Record = "Generate fields comments" + +caTitle :: CAResult -> Maybe Text +caTitle (CACodeAction CodeAction {_title}) = Just _title +caTitle _ = Nothing + +haddockCommentsPath :: String +haddockCommentsPath = "test/testdata/haddockComments" diff --git a/test/functional/Main.hs b/test/functional/Main.hs index ae458d788a..75adbad8fe 100644 --- a/test/functional/Main.hs +++ b/test/functional/Main.hs @@ -12,6 +12,7 @@ import Format import FunctionalBadProject import FunctionalCodeAction import FunctionalLiquid +import HaddockComments import HieBios import Highlight import ModuleName @@ -60,4 +61,5 @@ main = , Tactic.tests , TypeDefinition.tests , Splice.tests + , HaddockComments.tests ] diff --git a/test/testdata/haddockComments/ConstFunction.hs b/test/testdata/haddockComments/ConstFunction.hs new file mode 100644 index 0000000000..b5cc0d8246 --- /dev/null +++ b/test/testdata/haddockComments/ConstFunction.hs @@ -0,0 +1,4 @@ +module ConstFunction where + +f :: Int +f = 233 diff --git a/test/testdata/haddockComments/HigherRankFunction.hs b/test/testdata/haddockComments/HigherRankFunction.hs new file mode 100644 index 0000000000..7ed1de1e1b --- /dev/null +++ b/test/testdata/haddockComments/HigherRankFunction.hs @@ -0,0 +1,6 @@ +{-# LANGUAGE RankNTypes #-} + +module HigherRankFunction where + +f :: (forall a. [a] -> Int) -> [b] -> [c] -> (Int, Int) +f l xs ys = (l xs, l ys) diff --git a/test/testdata/haddockComments/HigherRankFunction.hs.expected b/test/testdata/haddockComments/HigherRankFunction.hs.expected new file mode 100644 index 0000000000..30aa4db284 --- /dev/null +++ b/test/testdata/haddockComments/HigherRankFunction.hs.expected @@ -0,0 +1,9 @@ +{-# LANGUAGE RankNTypes #-} + +module HigherRankFunction where + +f :: (forall a. [a] -> Int) -- ^ + -> [b] -- ^ + -> [c] -- ^ + -> (Int, Int) +f l xs ys = (l xs, l ys) diff --git a/test/testdata/haddockComments/KindSigFunction.hs b/test/testdata/haddockComments/KindSigFunction.hs new file mode 100644 index 0000000000..e4ea78c83c --- /dev/null +++ b/test/testdata/haddockComments/KindSigFunction.hs @@ -0,0 +1,11 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE KindSignatures #-} +{-# LANGUAGE PolyKinds #-} +{-# LANGUAGE RankNTypes #-} + +module KindSigFunction where + +import GHC.TypeLits + +f :: KnownSymbol k => (proxy :: k -> *) k -> String +f = symbolVal diff --git a/test/testdata/haddockComments/KindSigFunction.hs.expected b/test/testdata/haddockComments/KindSigFunction.hs.expected new file mode 100644 index 0000000000..de82c9bf7a --- /dev/null +++ b/test/testdata/haddockComments/KindSigFunction.hs.expected @@ -0,0 +1,12 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE KindSignatures #-} +{-# LANGUAGE PolyKinds #-} +{-# LANGUAGE RankNTypes #-} + +module KindSigFunction where + +import GHC.TypeLits + +f :: KnownSymbol k => (proxy :: k -> *) k -- ^ + -> String +f = symbolVal diff --git a/test/testdata/haddockComments/MultivariateFunction.hs b/test/testdata/haddockComments/MultivariateFunction.hs new file mode 100644 index 0000000000..48be5c7b0e --- /dev/null +++ b/test/testdata/haddockComments/MultivariateFunction.hs @@ -0,0 +1,4 @@ +module MultivariateFunction where + +f :: a -> b -> c -> d -> e -> f -> g -> g +f _ _ _ _ _ _ x = x diff --git a/test/testdata/haddockComments/MultivariateFunction.hs.expected b/test/testdata/haddockComments/MultivariateFunction.hs.expected new file mode 100644 index 0000000000..01d57bac65 --- /dev/null +++ b/test/testdata/haddockComments/MultivariateFunction.hs.expected @@ -0,0 +1,11 @@ +module MultivariateFunction where + +f :: a -- ^ + -> b -- ^ + -> c -- ^ + -> d -- ^ + -> e -- ^ + -> f -- ^ + -> g -- ^ + -> g +f _ _ _ _ _ _ x = x diff --git a/test/testdata/haddockComments/QualFunction.hs b/test/testdata/haddockComments/QualFunction.hs new file mode 100644 index 0000000000..a50ba560bc --- /dev/null +++ b/test/testdata/haddockComments/QualFunction.hs @@ -0,0 +1,4 @@ +module QualFunction where + +f :: (Show a, Show b) => a -> b -> String +f x y = show x <> show y diff --git a/test/testdata/haddockComments/QualFunction.hs.expected b/test/testdata/haddockComments/QualFunction.hs.expected new file mode 100644 index 0000000000..e91170424b --- /dev/null +++ b/test/testdata/haddockComments/QualFunction.hs.expected @@ -0,0 +1,6 @@ +module QualFunction where + +f :: (Show a, Show b) => a -- ^ + -> b -- ^ + -> String +f x y = show x <> show y diff --git a/test/testdata/haddockComments/Record.hs b/test/testdata/haddockComments/Record.hs new file mode 100644 index 0000000000..1adeb3f436 --- /dev/null +++ b/test/testdata/haddockComments/Record.hs @@ -0,0 +1,13 @@ +module Record where + +data Record a b c d e f + = RecordA + { a :: a, + b :: b + } + | Pair c d + | RecordB + { c :: e, + d :: f + } + | Void diff --git a/test/testdata/haddockComments/Record.hs.expected b/test/testdata/haddockComments/Record.hs.expected new file mode 100644 index 0000000000..ded7d3a388 --- /dev/null +++ b/test/testdata/haddockComments/Record.hs.expected @@ -0,0 +1,19 @@ +module Record where + +data Record a b c d e f + = RecordA + { +-- | +a :: a, +-- | +b :: b + } + | Pair c d + | RecordB + { +-- | +c :: e, +-- | +d :: f + } + | Void diff --git a/test/testdata/haddockComments/StaleFunction.hs b/test/testdata/haddockComments/StaleFunction.hs new file mode 100644 index 0000000000..266a23403e --- /dev/null +++ b/test/testdata/haddockComments/StaleFunction.hs @@ -0,0 +1,6 @@ +module StaleFunction where + +f :: a + -> b -- ^ ... + -> c -> c +f _ _ c = c diff --git a/test/testdata/haddockComments/StaleRecord.hs b/test/testdata/haddockComments/StaleRecord.hs new file mode 100644 index 0000000000..466db4c136 --- /dev/null +++ b/test/testdata/haddockComments/StaleRecord.hs @@ -0,0 +1,6 @@ +module StaleRecord where + +data Record = Record + { a :: Int, -- ^ ... + b :: String + } From a51086e7c0623e6a7c29cf77bb68c7cb3b1dfd67 Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Wed, 6 Jan 2021 13:04:22 +0800 Subject: [PATCH 05/10] Rename test data files, use golden test --- test/functional/HaddockComments.hs | 33 ++++++++++--------- ...xpected => HigherRankFunction.expected.hs} | 0 ...s.expected => KindSigFunction.expected.hs} | 0 ...ected => MultivariateFunction.expected.hs} | 0 ...n.hs.expected => QualFunction.expected.hs} | 0 ...{Record.hs.expected => Record.expected.hs} | 0 6 files changed, 18 insertions(+), 15 deletions(-) rename test/testdata/haddockComments/{HigherRankFunction.hs.expected => HigherRankFunction.expected.hs} (100%) rename test/testdata/haddockComments/{KindSigFunction.hs.expected => KindSigFunction.expected.hs} (100%) rename test/testdata/haddockComments/{MultivariateFunction.hs.expected => MultivariateFunction.expected.hs} (100%) rename test/testdata/haddockComments/{QualFunction.hs.expected => QualFunction.expected.hs} (100%) rename test/testdata/haddockComments/{Record.hs.expected => Record.expected.hs} (100%) diff --git a/test/functional/HaddockComments.hs b/test/functional/HaddockComments.hs index df6c0d3102..13f5f78c8e 100644 --- a/test/functional/HaddockComments.hs +++ b/test/functional/HaddockComments.hs @@ -9,43 +9,46 @@ module HaddockComments where import Control.Monad.IO.Class (liftIO) +import qualified Data.ByteString.Lazy as LBS import Data.Foldable (find) import Data.Maybe (mapMaybe) import Data.Text (Text) -import qualified Data.Text.IO as T +import Data.Text.Encoding (encodeUtf8) import Language.Haskell.LSP.Test import Language.Haskell.LSP.Types import System.FilePath ((<.>), ()) import Test.Hls.Util import Test.Tasty +import Test.Tasty.Golden import Test.Tasty.HUnit tests :: TestTree tests = testGroup "haddock comments" - [ normal "HigherRankFunction.hs" Signature 4 6, - normal "KindSigFunction.hs" Signature 9 10, - normal "MultivariateFunction.hs" Signature 2 8, - normal "QualFunction.hs" Signature 2 10, - normal "Record.hs" Record 7 2, - expectedNothing "StaleFunction.hs" Signature 3 3, - expectedNothing "StaleRecord.hs" Record 3 12 + [ goldenTest "HigherRankFunction" Signature 4 6, + goldenTest "KindSigFunction" Signature 9 10, + goldenTest "MultivariateFunction" Signature 2 8, + goldenTest "QualFunction" Signature 2 10, + goldenTest "Record.hs" Record 7 2, + expectedNothing "StaleFunction" Signature 3 3, + expectedNothing "StaleRecord" Record 3 12 ] -normal :: FilePath -> GenCommentsType -> Int -> Int -> TestTree -normal fp (toTitle -> expectedTitle) l c = testCase fp $ +goldenTest :: FilePath -> GenCommentsType -> Int -> Int -> TestTree +goldenTest fp (toTitle -> expectedTitle) l c = goldenVsString fp goldenFilePath $ runSession hlsCommand fullCaps haddockCommentsPath $ do - doc <- openDoc fp "haskell" + doc <- openDoc hsFilePath "haskell" _ <- waitForDiagnostics actions <- getCodeActions doc (Range (Position l c) (Position l $ succ c)) case find ((== Just expectedTitle) . caTitle) actions of Just (CACodeAction x) -> do executeCodeAction x - contentAfterAction <- documentContents doc - expected <- liftIO . T.readFile $ haddockCommentsPath fp <.> "expected" - liftIO $ contentAfterAction @?= expected + LBS.fromStrict . encodeUtf8 <$> documentContents doc _ -> liftIO $ assertFailure "Unable to find CodeAction" + where + hsFilePath = haddockCommentsPath fp <.> "hs" + goldenFilePath = haddockCommentsPath fp <.> "expected" <.> "hs" expectedNothing :: FilePath -> GenCommentsType -> Int -> Int -> TestTree expectedNothing fp (toTitle -> expectedTitle) l c = testCase fp $ @@ -66,4 +69,4 @@ caTitle (CACodeAction CodeAction {_title}) = Just _title caTitle _ = Nothing haddockCommentsPath :: String -haddockCommentsPath = "test/testdata/haddockComments" +haddockCommentsPath = "test" "testdata" "haddockComments" diff --git a/test/testdata/haddockComments/HigherRankFunction.hs.expected b/test/testdata/haddockComments/HigherRankFunction.expected.hs similarity index 100% rename from test/testdata/haddockComments/HigherRankFunction.hs.expected rename to test/testdata/haddockComments/HigherRankFunction.expected.hs diff --git a/test/testdata/haddockComments/KindSigFunction.hs.expected b/test/testdata/haddockComments/KindSigFunction.expected.hs similarity index 100% rename from test/testdata/haddockComments/KindSigFunction.hs.expected rename to test/testdata/haddockComments/KindSigFunction.expected.hs diff --git a/test/testdata/haddockComments/MultivariateFunction.hs.expected b/test/testdata/haddockComments/MultivariateFunction.expected.hs similarity index 100% rename from test/testdata/haddockComments/MultivariateFunction.hs.expected rename to test/testdata/haddockComments/MultivariateFunction.expected.hs diff --git a/test/testdata/haddockComments/QualFunction.hs.expected b/test/testdata/haddockComments/QualFunction.expected.hs similarity index 100% rename from test/testdata/haddockComments/QualFunction.hs.expected rename to test/testdata/haddockComments/QualFunction.expected.hs diff --git a/test/testdata/haddockComments/Record.hs.expected b/test/testdata/haddockComments/Record.expected.hs similarity index 100% rename from test/testdata/haddockComments/Record.hs.expected rename to test/testdata/haddockComments/Record.expected.hs From 7f4897091d21463674939dd118310c84d1dfd88d Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Wed, 6 Jan 2021 14:33:17 +0800 Subject: [PATCH 06/10] Fix paths of hs files in testing --- test/functional/HaddockComments.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/HaddockComments.hs b/test/functional/HaddockComments.hs index 13f5f78c8e..6752018470 100644 --- a/test/functional/HaddockComments.hs +++ b/test/functional/HaddockComments.hs @@ -47,7 +47,7 @@ goldenTest fp (toTitle -> expectedTitle) l c = goldenVsString fp goldenFilePath LBS.fromStrict . encodeUtf8 <$> documentContents doc _ -> liftIO $ assertFailure "Unable to find CodeAction" where - hsFilePath = haddockCommentsPath fp <.> "hs" + hsFilePath = fp <.> "hs" goldenFilePath = haddockCommentsPath fp <.> "expected" <.> "hs" expectedNothing :: FilePath -> GenCommentsType -> Int -> Int -> TestTree From b4c70d33e18244c712a7d02262a4bd9df3aec84e Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Wed, 6 Jan 2021 15:14:17 +0800 Subject: [PATCH 07/10] Add missing ConstFunction --- test/functional/HaddockComments.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/functional/HaddockComments.hs b/test/functional/HaddockComments.hs index 6752018470..df13ec149d 100644 --- a/test/functional/HaddockComments.hs +++ b/test/functional/HaddockComments.hs @@ -31,6 +31,7 @@ tests = goldenTest "MultivariateFunction" Signature 2 8, goldenTest "QualFunction" Signature 2 10, goldenTest "Record.hs" Record 7 2, + expectedNothing "ConstFunction" Signature 2 2, expectedNothing "StaleFunction" Signature 3 3, expectedNothing "StaleRecord" Record 3 12 ] From 0b1027015bbffea7efa4fb693e006eaa8fdbc142 Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Wed, 6 Jan 2021 15:17:27 +0800 Subject: [PATCH 08/10] Fix paths Fix paths --- test/functional/HaddockComments.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/HaddockComments.hs b/test/functional/HaddockComments.hs index df13ec149d..109ada95b3 100644 --- a/test/functional/HaddockComments.hs +++ b/test/functional/HaddockComments.hs @@ -30,7 +30,7 @@ tests = goldenTest "KindSigFunction" Signature 9 10, goldenTest "MultivariateFunction" Signature 2 8, goldenTest "QualFunction" Signature 2 10, - goldenTest "Record.hs" Record 7 2, + goldenTest "Record" Record 7 2, expectedNothing "ConstFunction" Signature 2 2, expectedNothing "StaleFunction" Signature 3 3, expectedNothing "StaleRecord" Record 3 12 @@ -54,7 +54,7 @@ goldenTest fp (toTitle -> expectedTitle) l c = goldenVsString fp goldenFilePath expectedNothing :: FilePath -> GenCommentsType -> Int -> Int -> TestTree expectedNothing fp (toTitle -> expectedTitle) l c = testCase fp $ runSession hlsCommand fullCaps haddockCommentsPath $ do - doc <- openDoc fp "haskell" + doc <- openDoc (fp <.> "hs") "haskell" _ <- waitForDiagnostics titles <- mapMaybe caTitle <$> getCodeActions doc (Range (Position l c) (Position l $ succ c)) liftIO $ expectedTitle `notElem` titles @? "Unexpected CodeAction" From 0296fcb3c0755c714c559d3697b8821c780962a9 Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Wed, 6 Jan 2021 15:47:54 +0800 Subject: [PATCH 09/10] Update annDeltaPos --- .../src/Ide/Plugin/HaddockComments.hs | 2 +- test/testdata/haddockComments/Record.expected.hs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs index bb296f5229..a7f4fa4a1b 100644 --- a/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs +++ b/plugins/hls-haddock-comments-plugin/src/Ide/Plugin/HaddockComments.hs @@ -97,7 +97,7 @@ genForRecord = GenComments {..} Just [x | (L _ ConDeclH98 {con_args = x}) <- cons] fromDecl _ = Nothing - updateAnn x = x {annEntryDelta = DP (1, -7), annPriorComments = [(comment, DP (1, -7))]} + updateAnn x = x {annEntryDelta = DP (1, 2), annPriorComments = [(comment, DP (1, 2))]} isFresh Ann {annPriorComments} = null annPriorComments diff --git a/test/testdata/haddockComments/Record.expected.hs b/test/testdata/haddockComments/Record.expected.hs index ded7d3a388..f7b0a379b8 100644 --- a/test/testdata/haddockComments/Record.expected.hs +++ b/test/testdata/haddockComments/Record.expected.hs @@ -3,17 +3,17 @@ module Record where data Record a b c d e f = RecordA { --- | -a :: a, --- | -b :: b + -- | + a :: a, + -- | + b :: b } | Pair c d | RecordB { --- | -c :: e, --- | -d :: f + -- | + c :: e, + -- | + d :: f } | Void From 3606cddaedfd69bf3bc20fd299eaa8d8f22d1baf Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Thu, 7 Jan 2021 12:53:24 +0800 Subject: [PATCH 10/10] Steal goldenGitDiff from class plugin --- test/functional/HaddockComments.hs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/functional/HaddockComments.hs b/test/functional/HaddockComments.hs index 109ada95b3..3aa36e514f 100644 --- a/test/functional/HaddockComments.hs +++ b/test/functional/HaddockComments.hs @@ -37,7 +37,7 @@ tests = ] goldenTest :: FilePath -> GenCommentsType -> Int -> Int -> TestTree -goldenTest fp (toTitle -> expectedTitle) l c = goldenVsString fp goldenFilePath $ +goldenTest fp (toTitle -> expectedTitle) l c = goldenVsStringDiff fp goldenGitDiff goldenFilePath $ runSession hlsCommand fullCaps haddockCommentsPath $ do doc <- openDoc hsFilePath "haskell" _ <- waitForDiagnostics @@ -71,3 +71,6 @@ caTitle _ = Nothing haddockCommentsPath :: String haddockCommentsPath = "test" "testdata" "haddockComments" + +goldenGitDiff :: FilePath -> FilePath -> [String] +goldenGitDiff fRef fNew = ["git", "diff", "--no-index", "--text", "--exit-code", fRef, fNew]