-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from Kroisse/docstr-annotation-unification
Merge docs into annotations completely
- Loading branch information
Showing
17 changed files
with
279 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,18 @@ | ||
module Nirum.Constructs.Declaration ( Declaration | ||
, Docs(Docs) | ||
, annotationDocsName | ||
, annotations | ||
, docs | ||
, name | ||
, toCode | ||
, toCodeWithPrefix | ||
, toText | ||
) where | ||
|
||
import Data.String (IsString(fromString)) | ||
|
||
import qualified Data.Text as T | ||
|
||
import Nirum.Constructs (Construct(toCode)) | ||
import Nirum.Constructs.Identifier (Identifier) | ||
import Nirum.Constructs (Construct) | ||
import Nirum.Constructs.Annotation (AnnotationSet, lookupDocs) | ||
import Nirum.Constructs.Docs (Docs) | ||
import Nirum.Constructs.Name (Name) | ||
|
||
annotationDocsName :: Identifier | ||
annotationDocsName = "docs" | ||
|
||
-- 'Construct' which has its own unique 'name' and can has its 'docs'. | ||
class Construct a => Declaration a where | ||
name :: a -> Name | ||
docs :: a -> Maybe Docs | ||
|
||
-- | Docstring for constructs. | ||
data Docs = Docs T.Text deriving (Eq, Ord, Show) | ||
|
||
-- | Convert the docs to text. | ||
toText :: Docs -> T.Text | ||
toText (Docs docs') = docs' | ||
|
||
-- | Similar to 'toCode' except it takes 'Maybe Docs' instead of 'Docs'. | ||
-- If the given docs is 'Nothing' it simply returns an empty string. | ||
-- Otherwise it returns a code string with the prefix. | ||
toCodeWithPrefix :: T.Text -- | The prefix to be prepended if not empty. | ||
-> Maybe Docs -- | The docs to convert to code. | ||
-> T.Text | ||
toCodeWithPrefix _ Nothing = "" | ||
toCodeWithPrefix prefix (Just docs') = T.append prefix $ toCode docs' | ||
|
||
instance Construct Docs where | ||
toCode (Docs docs') = let d = if "\n" `T.isSuffixOf` docs' | ||
then T.dropEnd 1 docs' | ||
else docs' | ||
in T.append "# " $ T.replace "\n" "\n# " d | ||
name :: a -> Name | ||
annotations :: a -> AnnotationSet | ||
|
||
instance IsString Docs where | ||
fromString s = let t = T.pack s | ||
in Docs (if "\n" `T.isSuffixOf` t | ||
then t | ||
else t `T.snoc` '\n') | ||
docs :: Declaration a => a -> Maybe Docs | ||
docs = lookupDocs . annotations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Nirum.Constructs.Docs ( Docs(Docs) | ||
, annotationDocsName | ||
, toCode | ||
, toCodeWithPrefix | ||
, toText | ||
) where | ||
|
||
import Data.String (IsString(fromString)) | ||
|
||
import qualified Data.Text as T | ||
|
||
import Nirum.Constructs (Construct(toCode)) | ||
import Nirum.Constructs.Identifier (Identifier) | ||
|
||
annotationDocsName :: Identifier | ||
annotationDocsName = "docs" | ||
|
||
-- | Docstring for constructs. | ||
data Docs = Docs T.Text deriving (Eq, Ord, Show) | ||
|
||
-- | Convert the docs to text. | ||
toText :: Docs -> T.Text | ||
toText (Docs docs') = docs' | ||
|
||
-- | Similar to 'toCode' except it takes 'Maybe Docs' instead of 'Docs'. | ||
-- If the given docs is 'Nothing' it simply returns an empty string. | ||
-- Otherwise it returns a code string with the prefix. | ||
toCodeWithPrefix :: T.Text -- | The prefix to be prepended if not empty. | ||
-> Maybe Docs -- | The docs to convert to code. | ||
-> T.Text | ||
toCodeWithPrefix _ Nothing = "" | ||
toCodeWithPrefix prefix (Just docs') = T.append prefix $ toCode docs' | ||
|
||
instance Construct Docs where | ||
toCode (Docs docs') = let d = if "\n" `T.isSuffixOf` docs' | ||
then T.dropEnd 1 docs' | ||
else docs' | ||
in T.append "# " $ T.replace "\n" "\n# " d | ||
|
||
instance IsString Docs where | ||
fromString s = let t = T.pack s | ||
in Docs (if "\n" `T.isSuffixOf` t | ||
then t | ||
else t `T.snoc` '\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.