lean4-code-actions
Version:
Refactorings and snippets for Lean 4
46 lines (34 loc) • 1.44 kB
text/typescript
import * as path from 'path'
import { sep } from 'path'
import { HieroName } from 'src/models/Lean/HieroName'
import { Name } from 'src/models/Lean/Name'
import { Uri, workspace } from 'vscode'
import { toString } from '../../models/Lean/HieroName'
import { Line } from '../text'
export const getNames = (uri: Uri) => {
const workspaceFolder = workspace.getWorkspaceFolder(uri)
if (!workspaceFolder) { throw new Error(`Cannot get a workspace folder for uri: "${uri}"`) }
const workspaceFolderPath = workspaceFolder.uri.fsPath
const relativeFilePath = path.parse(path.relative(workspaceFolderPath, uri.fsPath))
const names = relativeFilePath.dir.split(path.sep)
names.push(relativeFilePath.name)
return names.filter(ns => ns.length > 0)
}
export const ensureNames = (uri: Uri) => {
const namespaces = getNames(uri)
if (!namespaces) { throw new Error(`Cannot extract names from uri: "${uri}"`) }
return namespaces
}
export const toNamespace = (names: HieroName) => `namespace ${toString(names)}`
export const getNamespaceLines = (uri: Uri): Line[] => {
const splinters = getNames(uri)
if (!splinters) { return [] }
const childName = splinters.pop()
if (!childName) return []
return [toNamespace([childName])]
}
export function getRelativeFilePathFromLeanNames(names: Name[]) {
return names.join(sep) + '.lean'
}
export const toolchainMarker = 'leanprover'
export const packagesMarker = 'lake-packages'