Skip to content

Commit

Permalink
resolves #814 more restrictive glob search (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie authored Nov 12, 2023
1 parent 6ea142b commit 46d9af1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
23 changes: 19 additions & 4 deletions src/features/antora/antoraSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { disposeAll } from '../../util/dispose'
import * as nls from 'vscode-nls'
import ContentCatalog from '@antora/content-classifier/lib/content-catalog'
import { getWorkspaceFolder } from '../../util/workspace'
import { dir, exists } from '../../util/file'

const MAX_DEPTH_SEARCH_ANTORACONFIG = 100
const localize = nls.loadMessageBundle()

export interface AntoraResourceContext {
Expand Down Expand Up @@ -177,7 +179,7 @@ export async function findAntoraConfigFile (textDocumentUri: Uri): Promise<Uri |
cancellationToken.token.onCancellationRequested((e) => {
console.log('Cancellation requested, cause: ' + e)
})
const antoraConfigUris = await vscode.workspace.findFiles('**/antora.yml', '/node_modules/', 100, cancellationToken.token)
const antoraConfigUris = await vscode.workspace.findFiles('**/antora.yml', undefined, 100, cancellationToken.token)
// check for Antora configuration
for (const antoraConfigUri of antoraConfigUris) {
const antoraConfigParentDirPath = antoraConfigUri.path.slice(0, antoraConfigUri.path.lastIndexOf('/'))
Expand All @@ -193,15 +195,28 @@ export async function findAntoraConfigFile (textDocumentUri: Uri): Promise<Uri |
}

export async function antoraConfigFileExists (textDocumentUri: Uri): Promise<boolean> {
return await findAntoraConfigFile(textDocumentUri) !== undefined
const workspaceFolderUri = vscode.workspace.getWorkspaceFolder(textDocumentUri)?.uri
let currentDirectoryUri = dir(textDocumentUri, workspaceFolderUri)
let depth = 0
let antoraConfig: vscode.Uri
while (currentDirectoryUri !== undefined && depth < MAX_DEPTH_SEARCH_ANTORACONFIG) {
depth++
const antoraConfigUri = vscode.Uri.joinPath(currentDirectoryUri, 'antora.yml')
if (await exists(antoraConfigUri)) {
antoraConfig = antoraConfigUri
break
}
currentDirectoryUri = dir(currentDirectoryUri, workspaceFolderUri)
}
return antoraConfig !== undefined
}

export async function getAntoraConfigs (): Promise<AntoraConfig[]> {
const cancellationToken = new CancellationTokenSource()
cancellationToken.token.onCancellationRequested((e) => {
console.log('Cancellation requested, cause: ' + e)
})
const antoraConfigUris = await vscode.workspace.findFiles('**/antora.yml', '/node_modules/', 100, cancellationToken.token)
const antoraConfigUris = await vscode.workspace.findFiles('**/antora.yml', undefined, 100, cancellationToken.token)
// check for Antora configuration
const antoraConfigs = await Promise.all(antoraConfigUris.map(async (antoraConfigUri) => {
let config = {}
Expand Down Expand Up @@ -255,7 +270,7 @@ export async function getAntoraDocumentContext (textDocumentUri: Uri, workspaceS
.map(async (antoraConfig) => {
const workspaceFolder = getWorkspaceFolder(antoraConfig.uri)
const workspaceRelative = posixpath.relative(workspaceFolder.uri.path, antoraConfig.contentSourceRootPath)
const files = await Promise.all((await vscode.workspace.findFiles(workspaceRelative + '/modules/**/*')).map(async (file) => {
const files = await Promise.all((await vscode.workspace.findFiles(workspaceRelative + '/modules/*/{attachments,examples,images,pages,partials,assets}/**')).map(async (file) => {
const contentSourceRootPath = antoraConfig.contentSourceRootPath
return {
base: contentSourceRootPath,
Expand Down
21 changes: 1 addition & 20 deletions src/features/asciidoctorConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode'
import { Asciidoctor } from '@asciidoctor/core'
import { AsciidoctorProcessor } from '../asciidoctorProcessor'
import { dir, exists } from '../util/file'

const MAX_DEPTH_SEARCH_ASCIIDOCCONFIG = 100

Expand Down Expand Up @@ -44,26 +45,6 @@ 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 workspaceFolderUri = vscode.workspace.getWorkspaceFolder(documentUri)?.uri
let currentDirectoryUri = dir(documentUri, workspaceFolderUri)
Expand Down
20 changes: 20 additions & 0 deletions src/util/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,23 @@ export const sortFilesAndDirectories = (filesAndDirs: FileInfo[]): FileInfo[] =>
const files = filesAndDirs.filter((f) => f.isFile === true)
return [...dirs, ...files]
}

export 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('/')) })
}

export 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
}
}

0 comments on commit 46d9af1

Please sign in to comment.