mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-08 14:18:26 +02:00
Fix edit-in-kitty +lnum path
This commit is contained in:
@@ -912,6 +912,8 @@ class EditCmd:
|
|||||||
self.file_data = b''
|
self.file_data = b''
|
||||||
self.file_inode = -1, -1
|
self.file_inode = -1, -1
|
||||||
self.file_size = -1
|
self.file_size = -1
|
||||||
|
self.file_spec = ''
|
||||||
|
self.line_number = 0
|
||||||
self.version = 0
|
self.version = 0
|
||||||
self.source_window_id = self.editor_window_id = -1
|
self.source_window_id = self.editor_window_id = -1
|
||||||
self.abort_signaled = ''
|
self.abort_signaled = ''
|
||||||
@@ -934,15 +936,7 @@ class EditCmd:
|
|||||||
return
|
return
|
||||||
if self.version > 0:
|
if self.version > 0:
|
||||||
raise ValueError(f'Unsupported version received in edit protocol: {self.version}')
|
raise ValueError(f'Unsupported version received in edit protocol: {self.version}')
|
||||||
self.opts, extra_args = parse_opts_for_clone(['--type=overlay'] + self.args)
|
self.opts, _ = parse_opts_for_clone(['--type=overlay'] + self.args)
|
||||||
self.file_spec = extra_args.pop()
|
|
||||||
self.line_number = 0
|
|
||||||
import re
|
|
||||||
pat = re.compile(r'\+(-?\d+)')
|
|
||||||
for x in extra_args:
|
|
||||||
m = pat.match(x)
|
|
||||||
if m is not None:
|
|
||||||
self.line_number = int(m.group(1))
|
|
||||||
self.file_name = os.path.basename(self.file_spec)
|
self.file_name = os.path.basename(self.file_spec)
|
||||||
self.file_localpath = os.path.normpath(os.path.join(self.cwd, self.file_spec))
|
self.file_localpath = os.path.normpath(os.path.join(self.cwd, self.file_spec))
|
||||||
self.is_local_file = False
|
self.is_local_file = False
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ func edit_loop(data_to_send string, kill_if_signaled bool, on_data OnDataCallbac
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func edit_in_kitty(path string, opts *Options) (err error) {
|
func edit_in_kitty(path string, opts *Options, line_number int) (err error) {
|
||||||
read_file, err := os.Open(path)
|
read_file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to open %s for reading with error: %w", path, err)
|
return fmt.Errorf("Failed to open %s for reading with error: %w", path, err)
|
||||||
@@ -206,6 +206,8 @@ func edit_in_kitty(path string, opts *Options) (err error) {
|
|||||||
return fmt.Errorf("Failed to get the current working directory with error: %w", err)
|
return fmt.Errorf("Failed to get the current working directory with error: %w", err)
|
||||||
}
|
}
|
||||||
add_encoded("cwd", cwd)
|
add_encoded("cwd", cwd)
|
||||||
|
add_encoded("file_spec", path)
|
||||||
|
add_encoded("line_number", strconv.Itoa(line_number))
|
||||||
for _, arg := range os.Args[2:] {
|
for _, arg := range os.Args[2:] {
|
||||||
add_encoded("a", arg)
|
add_encoded("a", arg)
|
||||||
}
|
}
|
||||||
@@ -245,16 +247,28 @@ func EntryPoint(parent *cli.Command) *cli.Command {
|
|||||||
fmt.Fprintln(os.Stderr, "Usage:", cmd.Usage)
|
fmt.Fprintln(os.Stderr, "Usage:", cmd.Usage)
|
||||||
return 1, fmt.Errorf("No file to edit specified.")
|
return 1, fmt.Errorf("No file to edit specified.")
|
||||||
}
|
}
|
||||||
if len(args) != 1 {
|
lineNumber := 0
|
||||||
|
fileArgs := []string{}
|
||||||
|
for _, arg := range args {
|
||||||
|
if strings.HasPrefix(arg, "+") {
|
||||||
|
ln, err := strconv.Atoi(arg[1:])
|
||||||
|
if err == nil {
|
||||||
|
lineNumber = ln
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fileArgs = append(fileArgs, arg)
|
||||||
|
}
|
||||||
|
if len(fileArgs) != 1 {
|
||||||
fmt.Fprintln(os.Stderr, "Usage:", cmd.Usage)
|
fmt.Fprintln(os.Stderr, "Usage:", cmd.Usage)
|
||||||
return 1, fmt.Errorf("Only one file to edit must be specified")
|
return 1, fmt.Errorf("Only one file to edit must be specified, optionally with a line number")
|
||||||
}
|
}
|
||||||
var opts Options
|
var opts Options
|
||||||
err = cmd.GetOptionValues(&opts)
|
err = cmd.GetOptionValues(&opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 1, err
|
return 1, err
|
||||||
}
|
}
|
||||||
err = edit_in_kitty(args[0], &opts)
|
err = edit_in_kitty(fileArgs[0], &opts, lineNumber)
|
||||||
return 0, err
|
return 0, err
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user