Skip to content

Commit

Permalink
fire missing viewBlocked events
Browse files Browse the repository at this point in the history
the lifecycle of a text document, which may map to a file,
is unrelated to whether that document is opened in an editor,
and closing an editor does not reliably close the document

(it _currently_ does for documents opened with the `vscode.open` command,
but apparently that's an implementation detail we cannot rely on)

there is currently no event in vscode for when a file is opened in an editor

this change ensures that our own commands notice
when we're opening a blocked .envrc file,
and documents the limitation

related upstream issues:
- microsoft/vscode#138924
- microsoft/vscode#114005
- microsoft/vscode#15178
- microsoft/vscode#133532
  • Loading branch information
mkhl committed Dec 12, 2021
1 parent 4e583ae commit de3fbab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
### Fixed
- Handle null values exported by direnv
- Creating a new .envrc uses the intended file name
- Detect when a blocked .envrc is opened more reliably
### Added
- Offer to allow .envrc when saving it
- Show modified environment variable counts in status item
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ You will have to reload your environment manually for now.
Custom tasks with type `process` don't pick up on the modified environment.
Several task provider extensions provide these kinds of tasks.

Opening a blocked .envrc file without this extension's commands
(e.g. from the command line, the explorer, or the Open File dialog)
does not reliably ask to allow it.
[VSCode does not provide API to support this functionality yet.][vscode-15178]

[vscode-15178]: https://github.com/microsoft/vscode/issues/15178

direnv executes arbitrary shell scripts
so this extension requires trusted workspaces.

Expand Down
24 changes: 15 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ class Direnv implements vscode.Disposable {
}
}

async create() {
await this.open(await direnv.create())
}

async open(path?: string | undefined): Promise<void> {
path ??= await direnv.find()
const uri = await uriFor(path)
const doc = await vscode.workspace.openTextDocument(uri)
await vscode.window.showTextDocument(doc)
this.didOpen(path)
}

reload() {
this.willLoad.fire()
}
Expand Down Expand Up @@ -163,7 +175,7 @@ class Direnv implements vscode.Disposable {
await this.allow(path)
}
if (choice === 'View') {
await open(path)
await this.open(path)
}
}

Expand Down Expand Up @@ -198,12 +210,6 @@ async function uriFor(path: string): Promise<vscode.Uri> {
}
}

async function open(path: string): Promise<void> {
const uri = await uriFor(path)
const doc = await vscode.workspace.openTextDocument(uri)
await vscode.window.showTextDocument(doc)
}

export function activate(context: vscode.ExtensionContext) {
const environment = context.environmentVariableCollection
const statusItem = new status.Item(vscode.window.createStatusBarItem())
Expand All @@ -226,10 +232,10 @@ export function activate(context: vscode.ExtensionContext) {
}
}),
vscode.commands.registerCommand(command.Direnv.create, async () => {
await open(await direnv.create())
await instance.create()
}),
vscode.commands.registerCommand(command.Direnv.open, async () => {
await open(await direnv.find())
await instance.open()
}),
vscode.workspace.onDidOpenTextDocument((e) => {
instance.didOpen(e.fileName)
Expand Down

0 comments on commit de3fbab

Please sign in to comment.