-
Notifications
You must be signed in to change notification settings - Fork 2
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
Editor mode implemented #69
Open
DK318
wants to merge
7
commits into
main
Choose a base branch
from
diogo/editor-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5359cd2
Add editor mode
dcastro 6e9b9c7
Editor mode enhancement
DK318 526a1fa
fixup! Editor mode enhancement
DK318 992341a
Optional entry path in `create` command
DK318 1d40f98
Editor mode rollback + tags support
DK318 be67a10
HUnit and hedgehog tests fix
DK318 ed028d2
fixup! Editor mode rollback + tags support
DK318 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,162 @@ | ||
-- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io> | ||
-- | ||
-- SPDX-License-Identifier: MPL-2.0 | ||
|
||
module CLI.EditorMode where | ||
|
||
import CLI.EntryView | ||
import CLI.Types | ||
import Control.Lens | ||
import Data.Foldable (foldl') | ||
import Data.Maybe (fromMaybe) | ||
import Data.Text (Text) | ||
import Data.Text qualified as T | ||
import Data.Void (Void) | ||
import Fmt (pretty) | ||
import System.Environment (lookupEnv) | ||
import System.IO (SeekMode(AbsoluteSeek), hFlush, hGetContents, hPutStr, hSeek) | ||
import System.IO.Temp | ||
import System.Process as Process | ||
import Text.Interpolation.Nyan | ||
import Text.Megaparsec (ParseError, ParseErrorBundle, PosState) | ||
import Text.Megaparsec qualified as P | ||
|
||
data AnnotatedLine = AnnotatedLine | ||
{ _alLine :: Text | ||
, _alErrors :: [Text] | ||
} | ||
|
||
makeLenses 'AnnotatedLine | ||
|
||
mkAnnotatedLine :: Text -> AnnotatedLine | ||
mkAnnotatedLine t = AnnotatedLine t [] | ||
|
||
headerExample :: Text | ||
headerExample = [int|s| | ||
# Example: | ||
# | ||
# path = backend#/path/to/entry | ||
# | ||
# [fields] | ||
# public-field = public contents | ||
# private-field =~ private contents | ||
# multiline-thing = """ | ||
# multiline | ||
# contents | ||
# """ | ||
# | ||
# [tags] | ||
# first-tag | ||
# important | ||
|] | ||
|
||
renderEditorFile :: CreateOptions -> Text | ||
renderEditorFile opts = pretty entryView | ||
where | ||
publicFields = coFields opts <&> \field -> FieldInfoView field False | ||
privateFields = coPrivateFields opts <&> \field -> FieldInfoView field True | ||
entryView = EntryView (coQPath opts) (publicFields <> privateFields) (coTags opts) | ||
|
||
setOpts :: CreateOptions -> EntryView -> CreateOptions | ||
setOpts opts entryView = opts | ||
{ coQPath = qPath | ||
, coTags = tags | ||
, coFields = publicFields | ||
, coPrivateFields = privateFields | ||
} | ||
where | ||
qPath = entryView ^. mQEntryPath | ||
tags = entryView ^. entryTags | ||
publicFields = entryView ^.. fields . each . filtered (not . view private) . fieldInfo | ||
privateFields = entryView ^.. fields . each . filtered (view private) . fieldInfo | ||
|
||
editorMode :: CreateOptions -> IO CreateOptions | ||
editorMode opts = do | ||
editorEnvVar <- lookupEnv "EDITOR" <&> fromMaybe "vi" | ||
|
||
let | ||
go :: Text -> IO CreateOptions | ||
go editorFileContents = do | ||
withSystemTempFile "coffer" \fpath fhandle -> do | ||
-- Write fields to temp file. | ||
hPutStr fhandle $ T.unpack editorFileContents | ||
hFlush fhandle | ||
|
||
-- Launch editor. | ||
-- Note: The "editor" env variable may contain options/switches (e.g. `code --wait`), | ||
-- so we have to split those. | ||
let editorName = editorEnvVar ^?! to words . _head | ||
let editorArgs = editorEnvVar ^?! to words . _tail <> [fpath] | ||
putStrLn "Launching editor..." | ||
Process.callProcess editorName editorArgs | ||
|
||
-- Read temp file. | ||
hSeek fhandle AbsoluteSeek 0 | ||
editorFileContents' <- T.pack <$> hGetContents fhandle | ||
|
||
case P.parse parseEntryView fpath editorFileContents' of | ||
Right entryView -> do | ||
pure $ setOpts opts entryView | ||
Left errors -> do | ||
putStrLn "Failed to parse file." | ||
putStrLn $ P.errorBundlePretty errors | ||
go $ editorFileContents' | ||
& annotateEditorFile errors -- Add annotations for parsing errors | ||
& renderAnnotatedLines | ||
& T.strip | ||
|
||
go $ headerExample <> "\n\n" <> renderEditorFile opts | ||
|
||
-- | Remove all lines that begin with `#`. | ||
removeComments :: [AnnotatedLine] -> [AnnotatedLine] | ||
removeComments als = | ||
als & filter (\al -> al ^? alLine . _head /= Just '#') | ||
|
||
renderAnnotatedLines :: [AnnotatedLine] -> Text | ||
renderAnnotatedLines als = | ||
als | ||
<&> (\al -> T.intercalate "\n" (al ^. alLine : al ^. alErrors)) | ||
& T.unlines | ||
|
||
{- | For each error in the bunddle, adds a note with the parsing error | ||
next to the offending line. E.g.: | ||
> pw 1234 | ||
> # ^ | ||
> # unexpected '1' | ||
> # expecting '=' or white space | ||
-} | ||
annotateEditorFile :: ParseErrorBundle Text Void -> Text -> [AnnotatedLine] | ||
annotateEditorFile bundle fileContents = | ||
fileContents | ||
& T.lines | ||
-- Adding an extra empty line at the end. | ||
-- If a parsing error occurs at EOF, we can annotate this line. | ||
& (++ [""]) | ||
<&> mkAnnotatedLine | ||
& annotateLines bundle | ||
where | ||
mkAnnotatedLine :: Text -> AnnotatedLine | ||
mkAnnotatedLine t = AnnotatedLine t [] | ||
|
||
annotateLines :: ParseErrorBundle Text Void -> [AnnotatedLine] -> [AnnotatedLine] | ||
annotateLines bundle lines = | ||
fst $ | ||
foldl' annotateLine | ||
(lines, P.bundlePosState bundle) | ||
(P.bundleErrors bundle) | ||
|
||
-- | Finds the offending line, and adds one annotation with the parser error. | ||
annotateLine :: ([AnnotatedLine], PosState Text) -> ParseError Text Void -> ([AnnotatedLine], PosState Text) | ||
annotateLine (lines, posState) err = (lines', posState') | ||
where | ||
(_, posState') = P.reachOffset (P.errorOffset err) posState | ||
lineNumber = P.unPos (P.sourceLine $ P.pstateSourcePos posState') - 1 | ||
columnNumber = P.unPos (P.sourceColumn $ P.pstateSourcePos posState') - 1 | ||
errMsg = | ||
err | ||
& P.parseErrorTextPretty | ||
& T.pack | ||
& T.lines | ||
<&> mappend "# " | ||
caretLine = "#" <> T.replicate (columnNumber - 1) " " <> "^" | ||
lines' = lines & ix lineNumber . alErrors <>~ (caretLine : errMsg) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When user tries to save incorrect file multiple times, the same error description appends to the end of the existing one.
Example:
Maybe we could avoid this