Skip to content

Editing external module files to have a module docstring should increment that module's offset #6

@lmmx

Description

@lmmx

Editing external module files should increment the offset of the diagnostics in the module.

The current approach works for MVP (one global counter) but a proper solution should be:

  • Initialise a HashMap in which to map filepath strings to counter offset mappings
    • The value should be Vec<(i64, usize)>
  • If there is no entry for the filepath in the hashmap, consider its offset +0
  • All DiagnosticEntry with is_external_module_with_file and Some doc_comment will have 1+
    strings in a Vec: the length of this Vec is the baseline offset for the file. Give the line number
    of external mod declarations as 0 (line numbers are 1-based, so 0 is "before the first line").
  • Besides that, each diagnostic has a line start which is the value you key the length of their Vec
    if they likewise have Some doc_comment.

I would build that on every save (guaranteed once) not on every access.

  • Specifically in a method like fn build_offset_map(&self) -> HashMap<String, Vec<(i64, usize)>>
    • and call it when needed in apply_single_edit.

Then when you want to add a docstring to another diagnostic, you get the cumulative total offset
from the hashmap entry for the file in question (never need to consider this for 'external module
with file' docstrings).

For example, imagine a file src/foo.rs has:

  • a 10 line docstring specified as an external file mod foo declaration in src/lib.rs
  • a 1 line docstring on line 20 for struct Foo

has a cumulative total offset of +10 lines for lines 1-19 (i.e. from the first line to the line before the struct Foo).

Then from line 20 onwards it has a cumulative total of (+10 + +1), because we have both the module
docstring plus the struct docstring already added above.

So we can still use those original cargo
diagnostic line numbers we just need to adjust them.

  • If we want to add a docstring at line 15 we would write from the (15 + +10)'th line
  • If we want to add a docstring at line 25 we would write from the (25 + +10 + +1)'th line

And so on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions