Skip to content

Latest commit

 

History

History
103 lines (89 loc) · 7.86 KB

refactorings.md

File metadata and controls

103 lines (89 loc) · 7.86 KB

Refactorings in the language server

  • Status: Approved (2026-12-03, CR-299), with each open question settled as proposed.

The language server lists five refactorings (editor support, decision 5; Templates, code actions, refactorings): rename a component, inline a compmd component's use, turn a leaf into a container and back, extract a selection into a new component, and extract a component into a file of its own. The spec says what each construct means, but not what these edits write. These rules settle that, and the open questions at the end are the points that need the maintainer's word. Like every action of the server's, each one keeps the document valid: no new diagnostic is added, and an edit that would add one isn't offered, or is offered disabled with the reason (CodeAction.disabled).

Decisions

  1. Rename a component (textDocument/prepareRename and rename, at its name anywhere it is written: a use, a closer, a definition's or signature fence's component=, an import or export entry).

    • Defined in the document: every use and closer, its definition fence and its signature fence, and its md.export entry. For an exported component, see Open question 1.
    • Imported: only the local name changes, so the source is left alone. names: [card] becomes names: [{name: card, as: promo}], an existing as takes the new name, and a rename back to the export's name drops the as. The document's uses follow. In the frontmatter's syntax (YAML or TOML), the entry is rewritten in the form the organize actions write.
    • Refused (with the reason, as prepareRename's error): a built-in (@compmd/core: box, row, each, …), a name taken from names: "*" (Open question 2), a new name that isn't a name (spec §3.1), or one the document can already name.
    • A code renderer keeps its language (spec §6.3), so a rename changes its leaf uses only.
  2. Inline a use (refactor.inline, at a use of a compmd component the document defines or imports from a .comp.md file): the use is replaced by its definition's body, written as the source it expands to:

    • {{param}} becomes the argument's text, and its default where the use doesn't give it; {{md.slot}} and {{md.slot.<name>}} become the slot's content as written; {{md.fallback}} becomes the fallback;
    • relative headings are shifted to the level the use sits under (spec §7.2), unless the signature has headings: absolute;
    • the box: when the fence or the use has params for it, the body goes inside ::: box {…}, with the fence's params first and then the use's (spec §9.2); otherwise the body stands alone;
    • an inline use becomes its body's paragraph, as spec §7.2 renders it. It is offered disabled when the body needs the definition's scope: a param or md.slots used in an expression ((tone == "warn")), an argument given as an expression, or a body with definitions of its own. The definition is kept, since other uses may need it.
  3. A leaf into a container, and back (refactor.rewrite):

    • :: card[Text] {title="A"} becomes ::: card {title="A"}, Text and ::: /card, the fallback becoming the default slot's content. It is offered when the signature declares a default slot, or has no signature (a host component with derived props).
    • A container becomes a leaf when its slots hold at most one paragraph, all in the default slot. That paragraph becomes the fallback, so no content is lost. It isn't offered when a named slot has content or the body holds more than a paragraph.
    • Params, their continuation lines and any ::: + lines move as written.
  4. Extract a selection into a new component (refactor.extract; the name is Open question 3):

    • Blocks (the selection covers whole blocks): a compmd definition fence with slots: [], its body the selected text as written, placed after the last definition fence in the document, or before the first block after the frontmatter when there is none. The selection becomes the leaf :: name[<the selection's first sentence>], so the fallback reads as the text did.
    • Inline text (within one paragraph): the body is the selected text, and the selection becomes :name[<the selected text>]. Writing {{md.fallback}} as the body would keep only the text, so the selected text itself is the body.
    • Into a file of its own: the same definition, in <name>.comp.md beside the document (refused if that file exists), with md.export: {names: [<name>]} and an import of it added to the document (workspace/applyEdit's CreateFile and its text; Q4's lesson for editors that don't save, see Open question 4).
    • The extracted body keeps any interpolation of the document's own data ({{md.doc.title}}) as written. Since that would read the new document's data instead, the action is offered disabled when the selection uses md.doc or md.data.
  5. Extract a definition into a file of its own (refactor.extract, at a compmd definition fence): the fence, with its signature fence if it has one, moves to <name>.comp.md with md.export: {names: [<name>]}, and the document imports it. An md.export entry for it in the document stays, as a re-export (spec §6.2, which allows it).

  6. Across files. A rename that reaches other files (decision 1, an exported component) uses the project index (decision 15 of the language server) to find the documents that import this one by path. A pack published by name (export.pack) has importers the server can't see, so see Open question 1.

Open questions

Settled in the review (CR-299), each as proposed.

  1. Renaming an exported component. Proposed: rename it everywhere in the project. Each project file that imports it by path has its import entry renamed (an entry with as keeps its local name, so its uses don't change), and so do its uses. For a document with export.pack, whose importers aren't all in the project, the public name stays: the export becomes {name: <new>, as: <old>}. Alternative: always keep the public name with as, so a rename never reaches other files.
  2. A name from names: "*". Renaming it means listing it: names: "*" can't rename one name. Proposed: not offered, with the reason. Alternative: add a second import entry from the same source, {name: card, as: promo}, beside "*". That makes the old name visible as well as the new one.
  3. The new component's name (the language server's Q8, still open): myComponent (myComponent2 when taken), then the editor's rename at once where it can (editor.action.rename). Or the action asks for the name first (window/showMessageRequest can't take text, so this would be a command each editor package wires to its own input box).
  4. Creating a file. Neovim, Helix and Emacs left an applied edit's new file unsaved (Q4, CMD-463). Proposed: as for compmd.config.json, the server writes the new file itself and sends the document's own edit as a workspace edit. Alternative: a CreateFile in the workspace edit, accepting that those editors leave it unsaved.
  5. Leaf into container: the fallback as content. A leaf's fallback is what shows when the component can't render, and a container's default slot is content it renders. Proposed: the fallback becomes the default slot (decision 3), so nothing the writer wrote is lost. Alternative: an empty container, with the fallback dropped.

Tests

For each action, a test of each rule and each case where it isn't offered. Each test checks that the document processes before and after with no new diagnostic, and, for inline, that the expanded tree is the same before and after. The rename across files is tested on a project of three documents (one exporting, one importing with as, one with names: "*").