Skip to content

Commit

Permalink
feat: add option to disable context menu entry
Browse files Browse the repository at this point in the history
and hide the entry when editorconfig already exists
  • Loading branch information
SunsetTechuila committed Nov 27, 2024
1 parent b86ebd4 commit ea296cf
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 23 deletions.
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,25 @@
"type": "string",
"default": "default",
"description": "If generateAuto is false, this template path will be used for each newly-generated .editorconfig file."
},
"editorconfig.showMenuEntry": {
"type": "boolean",
"default": true,
"description": "Show the 'Generate .editorconfig' entry in the context menu of the Explorer view."
}
}
},
"menus": {
"commandPalette": [
{
"command": "EditorConfig.generate",
"when": "explorerResourceIsFolder"
"when": "explorerResourceIsFolder && config.editorconfig.showMenuEntry && editorconfig.showMenuEntry"
}
],
"explorer/context": [
{
"command": "EditorConfig.generate",
"when": "explorerResourceIsFolder",
"when": "explorerResourceIsFolder && config.editorconfig.showMenuEntry && editorconfig.showMenuEntry",
"group": "EditorConfig@1"
}
]
Expand Down Expand Up @@ -128,16 +133,13 @@
},
"scripts": {
"clean": "rimraf out",
"prebuild": "npm run clean",
"build": "tsc",
"postbuild": "cp -r src/test/suite/fixtures out/test/suite && cp -r src/test/untitled-suite/fixtures out/test/untitled-suite && cp src/DefaultTemplate.editorconfig out",
"lint": "eslint src/**/*.ts",
"pretest": "npm run lint && npm run build",
"prewatch": "npm run build",
"watch": "tsc -watch",
"check-types": "tsc --noEmit",
"test": "node out/test/runTest.js",
"vscode:prepublish": "npm run build"
"test": "node out/test/runTest.js"
},
"husky": {
"hooks": {
Expand Down
29 changes: 28 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import * as editorconfig from 'editorconfig'
import { TextDocument, TextEditorOptions, Uri, window, workspace } from 'vscode'
import {
TextDocument,
TextEditorOptions,
Uri,
FileType,
window,
workspace,
} from 'vscode'

/**
* Resolves `TextEditorOptions` for a `TextDocument`, combining the editor's
Expand Down Expand Up @@ -203,3 +210,23 @@ export function toEditorConfig(options: TextEditorOptions) {
return tabSize === 'auto' ? 4 : parseInt(String(tabSize), 10)
}
}

/**
* Get the URI of the .editorconfig file in the given folder
*/
export async function getEditorConfigUri(folderUri: Uri) {
const editorConfigUri = Uri.parse(`${folderUri.toString()}/.editorconfig`)

try {
const stats = await workspace.fs.stat(editorConfigUri)
if (stats.type === FileType.File) {
return editorConfigUri
}
} catch (err) {
if (err && err.name !== 'EntryNotFound (FileSystemError)') {
throw err
}
}

return
}
23 changes: 9 additions & 14 deletions src/commands/generateEditorConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { readFile as _readFile } from 'fs'
import { EOL } from 'os'
import { resolve } from 'path'
import { promisify } from 'util'
import { FileType, Uri, window, workspace } from 'vscode'
import { Uri, window, workspace } from 'vscode'
import { getEditorConfigUri } from '../api'

const readFile = promisify(_readFile)

Expand All @@ -19,27 +20,21 @@ export async function generateEditorConfig(uri: Uri) {
return
}

const editorConfigUri = Uri.parse(`${currentUri.toString()}/.editorconfig`)

try {
const stats = await workspace.fs.stat(editorConfigUri)
if (stats.type === FileType.File) {
if (await getEditorConfigUri(currentUri)) {
window.showErrorMessage(
'An .editorconfig file already exists in this workspace.',
)
return
}
} catch (err) {
if (err) {
if (err.name === 'EntryNotFound (FileSystemError)') {
writeFile()
} else {
window.showErrorMessage(err.message)
}
return
}
} catch (error) {
window.showErrorMessage(error.message)
return
}

const editorConfigUri = Uri.parse(`${currentUri.toString()}/.editorconfig`)
writeFile()

async function writeFile() {
const ec = workspace.getConfiguration('editorconfig')
const generateAuto = !!ec.get<boolean>('generateAuto')
Expand Down
41 changes: 39 additions & 2 deletions src/editorConfigMain.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { commands, DocumentSelector, ExtensionContext, languages } from 'vscode'
import {
commands,
DocumentSelector,
ExtensionContext,
languages,
workspace,
} from 'vscode'
import {
applyTextEditorOptions,
fromEditorConfig,
resolveCoreConfig,
resolveTextEditorOptions,
toEditorConfig,
getEditorConfigUri,
} from './api'
import { generateEditorConfig } from './commands/generateEditorConfig'
import DocumentWatcher from './DocumentWatcher'
Expand All @@ -13,7 +20,7 @@ import EditorConfigCompletionProvider from './EditorConfigCompletionProvider'
/**
* Main entry
*/
export function activate(ctx: ExtensionContext) {
export async function activate(ctx: ExtensionContext) {
ctx.subscriptions.push(new DocumentWatcher())

// register .editorconfig file completion provider
Expand All @@ -35,6 +42,13 @@ export function activate(ctx: ExtensionContext) {
}, 100)
})

await updateShowMenuEntryContext()
ctx.subscriptions.push(
workspace.onDidCreateFiles(updateShowMenuEntryContext),
workspace.onDidDeleteFiles(updateShowMenuEntryContext),
workspace.onDidRenameFiles(updateShowMenuEntryContext),
)

// register a command handler to generate a .editorconfig file
commands.registerCommand('EditorConfig.generate', generateEditorConfig)

Expand All @@ -46,3 +60,26 @@ export function activate(ctx: ExtensionContext) {
toEditorConfig,
}
}

async function updateShowMenuEntryContext() {
const workspaceUri =
workspace.workspaceFolders && workspace.workspaceFolders[0].uri
if (!workspaceUri) {
setShowMenuEntryContext(false)
return
}

try {
if (await getEditorConfigUri(workspaceUri)) {
setShowMenuEntryContext(false)
} else {
setShowMenuEntryContext(true)
}
} catch {
setShowMenuEntryContext(false)
}
}

function setShowMenuEntryContext(value: boolean) {
commands.executeCommand('setContext', 'editorconfig.showMenuEntry', value)
}

0 comments on commit ea296cf

Please sign in to comment.