Skip to content
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
13 changes: 12 additions & 1 deletion javascript/packages/language-server/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from "node:path"
import { existsSync, readFileSync } from "node:fs"
import { Connection, InitializeParams } from "vscode-languageserver/node"

Expand Down Expand Up @@ -57,7 +58,8 @@ export class Session {
} catch {
return null
}
}
},
documentPath => this.viewRootFor(documentPath),
)

this.projects = new Projects(this.connection, this.workspaceFolders, {
Expand Down Expand Up @@ -102,6 +104,15 @@ export class Session {
})
}

private viewRootFor(documentPath: string): string | null {
const project = this.projects.containing(documentPath)
const viewRoot = project?.partialIndexService.index?.viewRoot

if (!project || viewRoot === undefined) return null

return viewRoot === "." ? project.root : join(project.root, viewRoot)
}

async init() {
this.connection.console.log(`[Client] Diagnostic related information: ${this.capabilities.hasDiagnosticRelatedInformation ? "supported" : "not supported"}`)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ export class DefinitionProvider {
private parserService: ParserService
private exists: (filePath: string) => boolean
private read: (filePath: string) => string | null
private viewRootFor: (documentPath: string) => string | null

constructor(
parserService: ParserService,
exists: (filePath: string) => boolean,
read: (filePath: string) => string | null
read: (filePath: string) => string | null,
viewRootFor: (documentPath: string) => string | null = () => null
) {
this.parserService = parserService
this.exists = exists
this.read = read
this.viewRootFor = viewRootFor
}

getDefinition(document: TextDocument, position: Position): LocationLink[] {
Expand Down Expand Up @@ -374,8 +377,10 @@ export class DefinitionProvider {
}

private viewsRoot(documentPath: string): string | null {
const index = documentPath.lastIndexOf(VIEWS_DIRECTORY)
const indexed = this.viewRootFor(documentPath)
if (indexed !== null) return indexed

const index = documentPath.lastIndexOf(VIEWS_DIRECTORY)
if (index === -1) return null

return documentPath.slice(0, index + VIEWS_DIRECTORY.length - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,46 @@ describe("DefinitionProvider", () => {
expect(definitions(service, content, `"card"`, "file:///project/lib/templates/mailer.html.erb")).toEqual([])
})
})

describe("view root from the index", () => {
const TEMPLATES_URI = "file:///project/templates/events/show.html.erb"

function serviceRootedAt(viewRoot: string | null, ...files: string[]) {
const existing = new Set(files)

return new DefinitionProvider(
parserService,
filePath => existing.has(filePath),
() => "",
() => viewRoot,
)
}

it("resolves a qualified partial against the root the index reports", () => {
const service = serviceRootedAt("/project/templates", "/project/templates/shared/_header.html.erb")

expect(uris(service, `<%= render "shared/header" %>`, "shared/header", TEMPLATES_URI))
.toContain("file:///project/templates/shared/_header.html.erb")
})

it("finds nothing under a conventional layout the project does not use", () => {
const service = createService("/project/templates/shared/_header.html.erb")

expect(uris(service, `<%= render "shared/header" %>`, "shared/header", TEMPLATES_URI)).toEqual([])
})

it("treats a project root view root as the project root itself", () => {
const service = serviceRootedAt("/project", "/project/shared/_header.html.erb")

expect(uris(service, `<%= render "shared/header" %>`, "shared/header", "file:///project/events/show.html.erb"))
.toContain("file:///project/shared/_header.html.erb")
})

it("falls back to the conventional layout when nothing is indexed", () => {
const service = serviceRootedAt(null, "/project/app/views/shared/_header.html.erb")

expect(uris(service, `<%= render "shared/header" %>`, "shared/header"))
.toContain("file:///project/app/views/shared/_header.html.erb")
})
})
})
Loading