Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4481 filenames containing quotation marks #4482

Merged
merged 5 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/language/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path'
import { lw } from '../lw'
import { tokenizer } from '../utils/tokenizer'
import * as utils from '../utils/utils'
import { sanitizeInputFilePath } from '../utils/inputfilepath'

export class DefinitionProvider implements vscode.DefinitionProvider {
private async onAFilename(document: vscode.TextDocument, position: vscode.Position, token: string): Promise<string | undefined> {
Expand All @@ -18,7 +19,7 @@ export class DefinitionProvider implements vscode.DefinitionProvider {
}

if (line.match(regexDocumentclass)) {
return utils.resolveFile([path.dirname(vscode.window.activeTextEditor.document.fileName)], token, '.cls')
return utils.resolveFile([path.dirname(vscode.window.activeTextEditor.document.fileName)], sanitizeInputFilePath(token), '.cls')
}

let dirs: string[] = []
Expand All @@ -31,11 +32,11 @@ export class DefinitionProvider implements vscode.DefinitionProvider {

const result = line.match(regexImport)
if (result) {
dirs = [path.resolve(path.dirname(vscode.window.activeTextEditor.document.fileName), result[1])]
dirs = [path.resolve(path.dirname(vscode.window.activeTextEditor.document.fileName), sanitizeInputFilePath(result[1]))]
}

if (dirs.length > 0) {
return utils.resolveFile(dirs, token, '.tex')
return utils.resolveFile(dirs, sanitizeInputFilePath(token), '.tex')
}
return
}
Expand Down
14 changes: 7 additions & 7 deletions src/outline/structure/latex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type * as Ast from '@unified-latex/unified-latex-types'
import { lw } from '../../lw'
import { type TeXElement, TeXElementType } from '../../types'
import { resolveFile } from '../../utils/utils'
import { InputFileRegExp } from '../../utils/inputfilepath'
import { InputFileRegExp, sanitizeInputFilePath } from '../../utils/inputfilepath'


import { argContentToStr } from '../../utils/parser'
Expand Down Expand Up @@ -168,7 +168,7 @@ async function parseNode(
...attributes
}
} else if (node.type === 'macro' && ['input', 'InputIfFileExists', 'include', 'SweaveInput', 'subfile', 'loadglsentries', 'markdownInput'].includes(node.content)) {
const arg0 = argContentToStr(node.args?.[0]?.content || [])
const arg0 = sanitizeInputFilePath(argContentToStr(node.args?.[0]?.content || []))
const subFile = await resolveFile([ path.dirname(filePath), path.dirname(lw.root.file.path || ''), ...config.texDirs ], arg0)
if (subFile) {
element = {
Expand All @@ -182,8 +182,8 @@ async function parseNode(
}
}
} else if (node.type === 'macro' && ['import', 'inputfrom', 'includefrom'].includes(node.content)) {
const arg0 = argContentToStr(node.args?.[0]?.content || [])
const arg1 = argContentToStr(node.args?.[1]?.content || [])
const arg0 = sanitizeInputFilePath(argContentToStr(node.args?.[0]?.content || []))
const arg1 = sanitizeInputFilePath(argContentToStr(node.args?.[1]?.content || []))
const subFile = await resolveFile([ arg0, path.join(path.dirname(lw.root.file.path || ''), arg0 )], arg1)
if (subFile) {
element = {
Expand All @@ -197,8 +197,8 @@ async function parseNode(
}
}
} else if (node.type === 'macro' && ['subimport', 'subinputfrom', 'subincludefrom'].includes(node.content)) {
const arg0 = argContentToStr(node.args?.[0]?.content || [])
const arg1 = argContentToStr(node.args?.[1]?.content || [])
const arg0 = sanitizeInputFilePath(argContentToStr(node.args?.[0]?.content || []))
const arg1 = sanitizeInputFilePath(argContentToStr(node.args?.[1]?.content || []))
const subFile = await resolveFile([ path.dirname(filePath) ], path.join(arg0, arg1))
if (subFile) {
element = {
Expand Down Expand Up @@ -248,7 +248,7 @@ function insertSubFile(structs: FileStructureCache, struct?: TeXElement[], trave
if (lw.root.file.path === undefined) {
return []
}
struct = struct ?? structs[lw.root.file.path] ?? []
struct = JSON.parse(JSON.stringify(struct ?? structs[lw.root.file.path] ?? [])) as TeXElement[]
traversed = traversed ?? [lw.root.file.path]
let elements: TeXElement[] = []
for (const element of struct) {
Expand Down
21 changes: 15 additions & 6 deletions src/utils/inputfilepath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ enum MatchType {
interface MatchPath {
type: MatchType,
path: string,
directory: string,
directory?: string,
matchedString: string,
index: number
}
Expand Down Expand Up @@ -87,24 +87,33 @@ export class InputFileRegExp {
* @param rootFile
*/
static async parseInputFilePath(match: MatchPath, currentFile: string, rootFile: string): Promise<string | undefined> {

const rawTexDirs = vscode.workspace.getConfiguration('latex-workshop').get('latex.texDirs') as string[]
const texDirs = rawTexDirs.map((texDir) => {return replaceArgumentPlaceholders('', '')(texDir)})

const matchedDir = sanitizeInputFilePath(match.directory ?? '')
const matchedPath = sanitizeInputFilePath(match.path ?? '')
/* match of this.childReg */
if (match.type === MatchType.Child) {
return resolveFile([path.dirname(currentFile), path.dirname(rootFile), ...texDirs], match.path)
return resolveFile([path.dirname(currentFile), path.dirname(rootFile), ...texDirs], matchedPath)
}

/* match of this.inputReg */
if (match.type === MatchType.Input) {
if (match.matchedString.startsWith('\\subimport') || match.matchedString.startsWith('\\subinputfrom') || match.matchedString.startsWith('\\subincludefrom')) {
return resolveFile([path.dirname(currentFile)], path.join(match.directory, match.path))
return resolveFile([path.dirname(currentFile)], path.join(matchedDir, matchedPath))
} else if (match.matchedString.startsWith('\\import') || match.matchedString.startsWith('\\inputfrom') || match.matchedString.startsWith('\\includefrom')) {
return resolveFile([match.directory, path.join(path.dirname(rootFile), match.directory)], match.path)
return resolveFile([matchedDir, path.join(path.dirname(rootFile), matchedDir)], matchedPath)
} else {
return resolveFile([path.dirname(currentFile), path.dirname(rootFile), ...texDirs], match.path)
return resolveFile([path.dirname(currentFile), path.dirname(rootFile), ...texDirs], matchedPath)
}
}
return
}
}

export function sanitizeInputFilePath(filePath: string): string {
if (filePath.startsWith('"') && filePath.endsWith('"')) {
return filePath.slice(1, -1)
}
return filePath
}
Loading