Skip to content

Commit

Permalink
resolves #809 use a recursive approach to have better performance on …
Browse files Browse the repository at this point in the history
…large projects (#813)
  • Loading branch information
ggrossetie authored Nov 2, 2023
1 parent 45a9162 commit fec9b77
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Performance

- Replace `findFiles` by a recursive method that relies on `workspace.fs.stat` in the .asciidoctorconfig feature - should improve performance on large projects (#809)

## 3.1.3 (2023-07-21)

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 39 additions & 7 deletions src/features/asciidoctorConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as vscode from 'vscode'
import { Asciidoctor } from '@asciidoctor/core'
import { AsciidoctorProcessor } from '../asciidoctorProcessor'

const MAX_DEPTH_SEARCH_ASCIIDOCCONFIG = 100

export interface AsciidoctorConfigProvider {
activate(registry: Asciidoctor.Extensions.Registry, documentUri: vscode.Uri): Promise<void>;
}
Expand Down Expand Up @@ -42,14 +44,44 @@ export class AsciidoctorConfig implements AsciidoctorConfigProvider {
}
}

function dir (uri: vscode.Uri, workspaceFolder: vscode.Uri | undefined): vscode.Uri | undefined {
if (uri.path === workspaceFolder?.path) {
return undefined
}
if (uri.path.lastIndexOf('/') <= 0) {
return undefined
}
return uri.with({ path: uri.path.slice(0, uri.path.lastIndexOf('/')) })
}

async function exists (uri: vscode.Uri): Promise<boolean> {
try {
await vscode.workspace.fs.stat(uri)
return true
} catch (err) {
// file does not exist, ignore
return false
}
}

export async function getAsciidoctorConfigContent (documentUri: vscode.Uri): Promise<String | undefined> {
const asciidoctorConfigs = (await vscode.workspace.findFiles('**/.{asciidoctorconfig.adoc,asciidoctorconfig}', null))
.filter((uri) => {
const documentParentDirectory = documentUri.path.slice(0, documentUri.path.lastIndexOf('/'))
const asciidoctorConfigParentDirectory = uri.path.slice(0, uri.path.lastIndexOf('/'))
return documentParentDirectory.startsWith(asciidoctorConfigParentDirectory)
})
.sort((a, b) => a.path.localeCompare(b.path))
const workspaceFolderUri = vscode.workspace.getWorkspaceFolder(documentUri)?.uri
let currentDirectoryUri = dir(documentUri, workspaceFolderUri)
let depth = 0
const asciidoctorConfigs: vscode.Uri[] = []
while (currentDirectoryUri !== undefined && depth < MAX_DEPTH_SEARCH_ASCIIDOCCONFIG) {
depth++
const asciidoctorConfigAdocUri = vscode.Uri.joinPath(currentDirectoryUri, '.asciidoctorconfig.adoc')
if (await exists(asciidoctorConfigAdocUri)) {
asciidoctorConfigs.push(asciidoctorConfigAdocUri)
}
const asciidoctorConfigUri = vscode.Uri.joinPath(currentDirectoryUri, '.asciidoctorconfig')
if ((await exists(asciidoctorConfigUri))) {
asciidoctorConfigs.push(asciidoctorConfigUri)
}
currentDirectoryUri = dir(currentDirectoryUri, workspaceFolderUri)
}
asciidoctorConfigs.sort((a, b) => a.path.localeCompare(b.path))
if (asciidoctorConfigs.length === 0) {
return undefined
}
Expand Down

0 comments on commit fec9b77

Please sign in to comment.