Skip to content

Commit

Permalink
Improve generated edits with CRLF
Browse files Browse the repository at this point in the history
  • Loading branch information
nwolverson committed Mar 2, 2018
1 parent c54d585 commit 190e8c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/LanguageServer/Text.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ module LanguageServer.Text where
import Prelude

import Data.Array (findIndex, last, length, null, reverse, slice, zip)
import Data.Either (either)
import Data.Maybe (Maybe(..))
import Data.String (Pattern(..), joinWith, split)
import Data.String.Regex (regex)
import Data.String.Regex as Regex
import Data.String.Regex.Flags (noFlags)
import Data.Tuple (uncurry)
import LanguageServer.Types (DocumentUri, Position(..), Range(..), TextDocumentEdit(..), TextDocumentIdentifier(..), TextEdit(..), WorkspaceEdit, workspaceEdit)

Expand All @@ -19,8 +23,9 @@ makeWorkspaceEdit uri version range newText = workspaceEdit [ edit ]
-- | In particular the scenario of inserting text in the middle AC -> ABC becomes an edit of B only.
makeMinimalWorkspaceEdit :: DocumentUri -> Number -> String -> String -> Maybe WorkspaceEdit
makeMinimalWorkspaceEdit uri version oldText newText =
let newLines = split (Pattern "\n") newText
oldLines = case split (Pattern "\n") oldText of
let splitLines t = either (const [t]) (\r -> Regex.split r t) $ regex "\r?\n" noFlags
newLines = splitLines newText
oldLines = case splitLines oldText of
-- Add imports adds a newline to the end of the file always, giving bad diffs
xs | last xs /= Just "" && last newLines == Just "" -> xs <> [""]
xs -> xs
Expand Down
11 changes: 10 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,13 @@ main = runTest do
Assert.equal (Just [ mkEdit 0 1 "X\n"]) (getEdit <$> edit)
test "last line changed" do
let edit = makeMinimalWorkspaceEdit (DocumentUri "uri") 1.0 "A\nB\nC\n" "A\nB\nX\n"
Assert.equal (Just [ mkEdit 2 3 "X\n"]) (getEdit <$> edit)
Assert.equal (Just [ mkEdit 2 3 "X\n"]) (getEdit <$> edit)

test "CRLF" do
let edit = makeMinimalWorkspaceEdit (DocumentUri "uri") 1.0 "A\r\nC\r\n" "A\r\nB\r\nC\r\n"
Assert.equal (Just [ mkEdit 1 1 "B\n"]) (getEdit <$> edit)

test "CRLF old-only" do
-- If I have a CRLF file for some reason but IDE server gives me back LF
let edit = makeMinimalWorkspaceEdit (DocumentUri "uri") 1.0 "A\r\nC\r\n" "A\nB\nC\n"
Assert.equal (Just [ mkEdit 1 1 "B\n"]) (getEdit <$> edit)

0 comments on commit 190e8c1

Please sign in to comment.