Skip to content

Conversation

@heathhenley
Copy link
Collaborator

@heathhenley heathhenley commented Jan 17, 2025

An attempt at addressing: #16

I used the name Filec to avoid any conflict with the File constructor for the tree type in Fs here: https://github.com/chshersh/github-tui/blob/main/lib/fs/fs.mli#L12 (eg so that there's no confusion matching on Fs.File / Fs.Dir vs using Fs.File.t and the functions in there)

Copy link
Owner

@chshersh chshersh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome refactoring!

I like how different parts are now more separated, and we properly create modules with clear responsibilities.

I left a few minor comments about the naming and consistency of the interface but overall it looks great!

lib/fs/filec.mli Outdated
Comment on lines 10 to 20
(** Reads file contents using 'bat' to have pretty syntax highlighting **)
val read_file_contents : string -> t

(** Returns offset based on file type of contents **)
val offset_from_file_contents : t -> int

(** Returns the len of file contents **)
val line_len_from_file_contents : t -> int

(** Returns the lines of file contents **)
val lines_from_file_contents : t -> Pretty.doc array
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's simplify names of the functions here. Since they're prefixed with Filec, there's no need to add the file_contents suffix.

This is a typical trick in OCaml to use modules as kinda namespaces. So these functions will become:

Filec.read
Filec.offset
Filec.length
Filec.lines

lib/fs/filec.ml Outdated

let lines_from_file_contents = function
| Text { lines; _ } -> lines
| Binary -> [| Pretty.str binary_file_warning |]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do a minor performance optimisation:

Move [| Pretty.str binary_file_warning |] into a global variable and just return it here.

Generally, this is not safe because Arrays are mutable in OCaml. But we don't change this array, so I feel like it should be fine.

lib/fs/filec.ml Outdated
Comment on lines 40 to 42
let line_len_from_file_contents = function
| Text { lines; _ } -> Array.length lines
| Binary -> 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would simplify this function by calling Filec.lines first and then Array.length on the result.

This way the Binary case will be automatically consistent with how many lines are inside and the interface will be less error-prone.

@chshersh
Copy link
Owner

I used the name Filec to avoid any conflict with the File constructor for the tree type in Fs here: https://github.com/chshersh/github-tui/blob/main/lib/fs/fs.mli#L12 (eg so that there's no confusion matching on Fs.File / Fs.Dir vs using Fs.File.t and the functions in there)

Good idea!

There also could be some standard module File because it's a quite a common name so yes, great idea to avoid conflicts with common names.

@chshersh chshersh added type: refactoring Improving the code quality w/o changing functionality component: DX Linters, CI, developer experience improvements labels Jan 18, 2025
@chshersh chshersh mentioned this pull request Jan 18, 2025
@heathhenley
Copy link
Collaborator Author

I think I addressed the suggestions you pointed out, lmk what you think!

Copy link
Owner

@chshersh chshersh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff!

I left only one minor suggestion but overall it looks great!

@heathhenley heathhenley force-pushed the dev_lunchtime_caml_practice branch from 1da7dd1 to 1c5ac0e Compare January 20, 2025 22:34
Co-authored-by: Dmitrii Kovanikov <[email protected]>
@heathhenley
Copy link
Collaborator Author

Rebased and applied the latest suggestion ✅

Copy link
Owner

@chshersh chshersh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing! 🏆

@chshersh chshersh merged commit 2359f24 into chshersh:main Jan 21, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: DX Linters, CI, developer experience improvements type: refactoring Improving the code quality w/o changing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants