Skip to content

Commit

Permalink
Add error message for diff viewer (#2255)
Browse files Browse the repository at this point in the history
  • Loading branch information
fox0430 authored Mar 1, 2025
1 parent f47bf8b commit f44d27d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. _#2255: https://github.com/fox0430/moe/pull/2255

Added
.....

- `#2255`_ Add error message for diff viewer

20 changes: 13 additions & 7 deletions src/moepkg/diffviewerutils.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[###################### GNU General Public License 3.0 ######################]#
# #
# Copyright (C) 2017─2023 Shuhei Nogawa #
# Copyright (C) 2017─2025 Shuhei Nogawa #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
Expand All @@ -18,18 +18,24 @@
#[############################################################################]#

import std/[strformat, osproc, strutils]

import pkg/results

import unicodeext, highlight, color

proc initDiffViewerBuffer*(sourceFilePath, backupFilePath: string): seq[Runes] =
proc initDiffViewerBuffer*(
sourceFilePath, backupFilePath: string
): Result[seq[Runes], string] =
let cmdResult = execCmdEx(fmt"diff -u {sourceFilePath} {backupFilePath}")
# The diff command return 2 on failure.
if cmdResult.exitCode == 2:
# TODO: Write the error message to the command window.
return @[ru""]
# The diff command return 2 on failure.
return Result[seq[Runes], string].err "diff command failed"

result = @[ru""]
var r = @[ru""]
for line in cmdResult.output.splitLines:
result.add line.toRunes
r.add line.toRunes

return Result[seq[Runes], string].ok r

proc initDiffViewerHighlight*(buffer: seq[Runes]): Highlight =
for i, line in buffer:
Expand Down
10 changes: 7 additions & 3 deletions src/moepkg/editorstatus.nim
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,13 @@ proc addNewBuffer*(
backupDir = backupDir(baseBackupDir, sourceFilePath)
backupFilePath = backupDir / $currentLineBuffer
status.bufStatus.add initBufferStatus(mode).get
status.bufStatus[^1].buffer =
initDiffViewerBuffer(sourceFilePath, backupFilePath).toGapBuffer
status.bufStatus[^1].path = backupFilePath.toRunes

let diffResult = initDiffViewerBuffer(sourceFilePath, backupFilePath)
if diffResult.isOk:
status.bufStatus[^1].buffer = diffResult.get.toGapBuffer
status.bufStatus[^1].path = backupFilePath.toRunes
else:
status.commandLine.writeDiffViewerError(diffResult.error)
else:
let b = initBufferStatus(path, mode)
if b.isOk:
Expand Down
4 changes: 4 additions & 0 deletions src/moepkg/messages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ proc writeBufferChangedWarn*(commandLine: var CommandLine, filename: Runes) =
let mess = fmt"File {filename} has changed and the buffer was changed in Moe as well."
commandLine.writeWarn(mess)

proc writeDiffViewerError*(commandLine: var CommandLine, message: string) =
let mess = fmt"Error: diff: ${message}"
commandLine.writeError(mess)

proc writeLspError*(commandLine: var CommandLine, message: string) =
let mess = fmt"lsp: {message}"
commandLine.writeError(mess)
Expand Down

0 comments on commit f44d27d

Please sign in to comment.