{"id":13995842,"url":"https://github.com/DenisGorbachev/vscode-lean4-code-actions","last_synced_at":"2025-07-22T22:32:45.820Z","repository":{"id":165054093,"uuid":"640425778","full_name":"DenisGorbachev/vscode-lean4-code-actions","owner":"DenisGorbachev","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-29T13:19:41.000Z","size":8213,"stargazers_count":6,"open_issues_count":11,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-11-15T20:52:19.759Z","etag":null,"topics":["lean","vscode","vscode-extension"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DenisGorbachev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-14T03:34:01.000Z","updated_at":"2024-11-06T02:45:22.000Z","dependencies_parsed_at":"2024-04-07T01:54:13.120Z","dependency_job_id":null,"html_url":"https://github.com/DenisGorbachev/vscode-lean4-code-actions","commit_stats":null,"previous_names":["denisgorbachev/vscode-lean4-code-actions"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenisGorbachev%2Fvscode-lean4-code-actions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenisGorbachev%2Fvscode-lean4-code-actions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenisGorbachev%2Fvscode-lean4-code-actions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenisGorbachev%2Fvscode-lean4-code-actions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DenisGorbachev","download_url":"https://codeload.github.com/DenisGorbachev/vscode-lean4-code-actions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227190434,"owners_count":17745264,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lean","vscode","vscode-extension"],"created_at":"2024-08-09T14:03:36.853Z","updated_at":"2024-11-29T18:31:11.452Z","avatar_url":"https://github.com/DenisGorbachev.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# VSCode Lean 4 Code Actions\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./img/autoImport.gif\"/\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\" style=\"padding: 20px 0\"\u003e\n  \u003ca href=\"https://marketplace.visualstudio.com/items?itemName=denis-gorbachev.lean4-code-actions\u0026ssr=false\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Install-VSCode%20Marketplace-blue\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Installation\n\n* [Install the extension](https://marketplace.visualstudio.com/items?itemName=denis-gorbachev.lean4-code-actions\u0026ssr=false)\n* Add the keyboard shortcuts for [useful actions](#actions)\n\nNote: a custom language configuration is available as [a separate extension](https://github.com/DenisGorbachev/vscode-lean4-language-configuration#readme).\n\n## Actions\n\n* [Create a new file](#create-a-new-file)\n* [Auto-import a definition](#auto-import)\n* [Update imports on rename](#update-imports-on-rename)\n* [Set argument style](#set-argument-style)\n* [Extract a definition to a separate file](#extract-a-definition-to-a-separate-file)\n* [Find-replace the current word within a code block](#find-replace-the-current-word-within-a-code-block)\n* [Convert a text block to a list of strings](#convert-a-text-block-to-a-list-of-strings)\n\n**Disclaimer:** the commands currently operate directly on text. As such, they have many limitations - for example, sometimes they don't properly detect Lean names. We have plans to reimplement the commands as proper code actions within LSP that operate on `Syntax` instead of text.\n\n## Snippets\n\n* `imp` - expands to [configured imports](#configuration-options)\n* `op` - expands to [configured opens](#configuration-options)\n* `ns` - expands to `namespace ${currentFileName}`\n* `nsp` - expands to `namespace ${currentFilePath}`\n* `var` - expands to `variable (${name} : ${Type})`\n* `ind` - expands to `inductive` declaration\n* `struct` - expands to `structure` declaration\n* `cls` - expands to `class` declaration\n\n## Configuration options\n\n* `lean4CodeActions.registerRenameProvider` - use this extension as a rename provider for `.lean` files\n* `lean4CodeActions.updateImportsOnFileRename` - update imports in other files when a file is renamed\n* `lean4CodeActions.namespace.prefix` - a prefix for top-level namespaces in generated code (added as `${prefix}.${body}`)\n* `lean4CodeActions.createNewFile.imports` - a list of Lean filenames to be imported.\n* `lean4CodeActions.createNewFile.opens` - a list of Lean namespaces to be opened.\n* `lean4CodeActions.createNewFile.derivings` - a list of Lean names to be derived.\n* `lean4CodeActions.defaultLib` - the default library to be used when creating a new file. If it's empty, the extension will ask you to choose the library interactively.\n\n## Related work\n\n* [Std](https://github.com/leanprover/std4) already contains some [code actions](https://github.com/leanprover/std4/tree/main/Std/CodeAction)\n\n### Create a new file\n\n\u003cimg src=\"./img/createNewFile.gif\"/\u003e\n\n**Before:**\n\n(Nothing)\n\n**After:**\n\nFile: `CodeActions/Test/CreateNewFile/User.lean`\n\n```lean\nnamespace CodeActions.Test.CreateNewFile\n\nstructure User where\n\nderiving Repr, Inhabited\n\nnamespace User\n```\n\n**Notes:**\n\n* This command supports adding `import`, `open` and `deriving instance` commands via [configuration](#configuration-options).\n\n---\n\n### Auto-import\n\n\u003cimg src=\"./img/autoImport.gif\" /\u003e\n\n**Before:**\n\n```lean\ndef x : Rat := 1.0\n```\n\n**After:**\n\n```lean\nimport Std.Data.Rat.Basic\n\ndef x : Rat := 1.0\n```\n\n**Gotchas:**\n\n* If you execute this command with an empty selection (just a cursor on the name), then only the part captured by [`getWordRangeAtPosition`](https://code.visualstudio.com/api/references/vscode-api#TextDocument.getWordRangeAtPosition) will be used. To import a hierarchical name, select it fully, then execute the command. Alternatively, you can enable detection of hierarchical names by installing [a custom language configuration](https://marketplace.visualstudio.com/items?itemName=denis-gorbachev.lean4-language-configuration).\n\n---\n\n### Update imports on rename\n\n\u003cimg src=\"./img/updateImports.gif\" /\u003e\n\nWhen you rename a file (or move it to another folder), it updates the imports in other files.\n\n**Before:**\n\nFile 1: `CodeActions/Test/UpdateImports/Child.lean`\n\n```lean\nnamespace CodeActions.Test.UpdateImports.Child\n\ndef x : Nat := 1\n```\n\nFile 2: `CodeActions/Test/UpdateImports/Parent.lean`\n\n```lean\nimport CodeActions.Test.UpdateImports.Child\n\nnamespace CodeActions.Test.UpdateImports.Parent\n\ndef y : Nat := 2 * Child.x\n```\n\n**After:**\n\nFile 1: `CodeActions/Test/UpdateImports/Nested/RenamedChild.lean`\n\n```lean\nnamespace CodeActions.Test.UpdateImports.Child\n\ndef x : Nat := 1\n```\n\nFile 2: `CodeActions/Test/UpdateImports/Parent.lean`\n\n```lean\nimport CodeActions.Test.UpdateImports.Nested.RenamedChild\n\nnamespace CodeActions.Test.UpdateImports.Parent\n\ndef y : Nat := 2 * Child.x\n```\n\n**Notes:**\n\n* This is a listener, not a command - it is executed automatically upon a file rename. It works even if you rename a file via another extension ([File Utils](https://marketplace.visualstudio.com/items?itemName=sleistner.vscode-fileutils), [File Bunny](https://marketplace.visualstudio.com/items?itemName=robole.file-bunny)).\n* It doesn't update the namespaces (should be done manually).\n* It can be disabled by setting `lean4CodeActions.updateImportsOnFileRename` to `false`\n\n---\n\n### Set argument style\n\n\u003cimg src=\"./img/setArgumentStyle.gif\" /\u003e\n\n**Before:**\n\n```lean\n(α : Type u)\n```\n\n**After:**\n\n```lean\n{α : Type u}\n```\n\n**Notes:**\n\n* The command supports four argument styles: [explicit](https://leanprover.github.io/theorem_proving_in_lean4/dependent_type_theory.html#function-abstraction-and-evaluation), [implicit strong](https://leanprover.github.io/theorem_proving_in_lean4/dependent_type_theory.html#implicit-arguments), [implicit weak](https://leanprover.github.io/theorem_proving_in_lean4/interacting_with_lean.html?#more-on-implicit-arguments), [typeclass](https://leanprover.github.io/theorem_proving_in_lean4/type_classes.html) (`()`, `{}`, `⦃⦄`, `[]`).\n\n---\n\n### Extract a definition to a separate file\n\n\u003cimg src=\"./img/extractDefinitionToSeparateFile.gif\"/\u003e\n\n**Before:**\n\nFile 1: `CodeActions/Test/ExtractDefinition/Book.lean`\n\n```lean\nnamespace CodeActions.Test.ExtractDefinition\n\nstructure Author where\n  name : String\n\nstructure Book where\n  authors : List Author \n```\n\n**After:**\n\nFile 1: `CodeActions/Test/ExtractDefinition/Book.lean`\n\n```lean\nimport CodeActions.Test.ExtractDefinition.Author\n\nnamespace CodeActions.Test.ExtractDefinition\n\nstructure Book where\n  authors : List Author \n```\n\nFile 2: `CodeActions/Test/ExtractDefinition/Author.lean`\n\n```lean\nnamespace CodeActions.Test.ExtractDefinition\n\nstructure Author where\n  name : String\n\nnamespace Author\n```\n\n**How it works:**\n\n* It extracts a definition into a separate file\n* It adds an import to the original file\n\n**Gotchas:**\n\n* It doesn't add the `open` command yet\n\n---\n\n### Find-replace the current word within a code block\n\n\u003cimg src=\"./img/rename.gif\" /\u003e\n\n**Before:**\n\n```lean\ndef foo : IO String := do\n  let text ← IO.FS.readFile \"/tmp/secrets\"\n  return text\n```\n\n**After:**\n\n```lean\ndef foo : IO String := do\n  let secrets ← IO.FS.readFile \"/tmp/secrets\"\n  return secrets\n```\n\nYou can use it to rename a local binding (if the variable name is a unique string of characters across the code block).\n\n**Gotchas:**\n\n* It's a simple find-replace: it doesn't distinguish between variables and text within strings, for example.\n* It's activated via \"Rename Symbol\" native command. If it causes problems, you can disable it by setting `lean4CodeActions.registerRenameProvider` to `false` in the extension configuration.\n* It relies on [`getWordRangeAtPosition`](https://code.visualstudio.com/api/references/vscode-api#TextDocument.getWordRangeAtPosition) to detect the word under cursor. You can improve the detection by installing [a custom language configuration](https://marketplace.visualstudio.com/items?itemName=denis-gorbachev.lean4-language-configuration).\n\n**Notes:**\n\n* A code block is defined as a continuous list of non-blank lines.\n\n---\n\n### Convert a text block to a list of strings\n\n\u003cimg src=\"./img/convertTextToList.gif\" /\u003e\n\n**Before:**\n\n```text\nfoo\nbar\nxyz\n```\n\n**After:**\n\n```text\n\"foo\",\n\"bar\",\n\"xyz\"\n```\n\nEach line becomes an element of the list.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDenisGorbachev%2Fvscode-lean4-code-actions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDenisGorbachev%2Fvscode-lean4-code-actions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDenisGorbachev%2Fvscode-lean4-code-actions/lists"}