Skip to content
Merged
Changes from 2 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
72 changes: 61 additions & 11 deletions packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,42 @@ import { useTerminalDimensions } from "@opentui/solid"
import { Locale } from "@/util/locale"
import type { PromptInfo } from "./history"

function removeLineRange(input: string) {
const hashIndex = input.lastIndexOf("#")
return hashIndex !== -1 ? input.substring(0, hashIndex) : input
}

function extractLineRange(input: string) {
const hashIndex = input.lastIndexOf("#")
if (hashIndex === -1) {
return { baseQuery: input }
}

const baseName = input.substring(0, hashIndex)
const linePart = input.substring(hashIndex + 1)
const lineMatch = linePart.match(/^(\d+)(?:-(\d+))?$/)

if (!lineMatch) {
return { baseQuery: baseName }
}

const startLine = Number(lineMatch[1])
const endLine = lineMatch[2] ? Number(lineMatch[2]) : undefined

if (endLine !== undefined && startLine > endLine) {
return { baseQuery: baseName }
}

return {
lineRange: {
baseName,
startLine,
endLine,
},
baseQuery: baseName,
}
}

export type AutocompleteRef = {
onInput: (value: string) => void
onKeyDown: (e: KeyEvent) => void
Expand Down Expand Up @@ -142,9 +178,11 @@ export function Autocomplete(props: {
async (query) => {
if (!store.visible || store.visible === "/") return []

const { lineRange, baseQuery } = extractLineRange(query ?? "")

// Get files from SDK
const result = await sdk.client.find.files({
query: query ?? "",
query: baseQuery,
})

const options: AutocompleteOption[] = []
Expand All @@ -153,15 +191,27 @@ export function Autocomplete(props: {
if (!result.error && result.data) {
const width = props.anchor().width - 4
options.push(
...result.data.map(
(item): AutocompleteOption => ({
display: Locale.truncateMiddle(item, width),
...result.data.map((item): AutocompleteOption => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style suggestion: The let statements for url and filename could be avoided using an IIFE pattern (see packages/opencode/src/util.iife.ts for reference). However, this is minor and the current implementation is still readable - feel free to keep as-is if you prefer the simplicity.

let url = `file://${process.cwd()}/${item}`
let filename = item
if (lineRange && !item.endsWith("/")) {
filename = `${item}#${lineRange.startLine}${lineRange.endLine ? `-${lineRange.endLine}` : ""}`
const urlObj = new URL(url)
urlObj.searchParams.set("start", String(lineRange.startLine))
if (lineRange.endLine !== undefined) {
urlObj.searchParams.set("end", String(lineRange.endLine))
}
url = urlObj.toString()
}

return {
display: Locale.truncateMiddle(filename, width),
onSelect: () => {
insertPart(item, {
insertPart(filename, {
type: "file",
mime: "text/plain",
filename: item,
url: `file://${process.cwd()}/${item}`,
filename,
url,
source: {
type: "file",
text: {
Expand All @@ -173,8 +223,8 @@ export function Autocomplete(props: {
},
})
},
}),
),
}
}),
)
}

Expand Down Expand Up @@ -383,8 +433,8 @@ export function Autocomplete(props: {
return prev
}

const result = fuzzysort.go(currentFilter, mixed, {
keys: [(obj) => obj.display.trimEnd(), "description", (obj) => obj.aliases?.join(" ") ?? ""],
const result = fuzzysort.go(removeLineRange(currentFilter), mixed, {
keys: [(obj) => removeLineRange(obj.display.trimEnd()), "description", (obj) => obj.aliases?.join(" ") ?? ""],
limit: 10,
scoreFn: (objResults) => {
const displayResult = objResults[0]
Expand Down