Skip to content

Commit

Permalink
Merge pull request #3554 from icycodes/chore-cherry-pick-3534-3551-to…
Browse files Browse the repository at this point in the history
…-r0.21

chore: cherry pick 3534 3551 to r0.21
  • Loading branch information
wsxiaoys authored Dec 13, 2024
2 parents 9752656 + 74578d9 commit fbd2de5
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 152 deletions.
2 changes: 1 addition & 1 deletion clients/tabby-chat-panel/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tabby-chat-panel",
"type": "module",
"version": "0.3.0",
"version": "0.4.0",
"keywords": [],
"sideEffects": false,
"exports": {
Expand Down
173 changes: 151 additions & 22 deletions clients/tabby-chat-panel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,54 @@ import { version } from '../package.json'

export const TABBY_CHAT_PANEL_API_VERSION: string = version

/**
* Represents a position in a file.
*/
export interface Position {
/**
* 1-based line number
*/
line: number
/**
* 1-based character number
*/
character: number
}

/**
* Represents a range in a file.
*/
export interface PositionRange {
/**
* The start position of the range.
*/
start: Position
/**
* The end position of the range.
*/
end: Position
}

/**
* Represents a range of lines in a file.
*/
export interface LineRange {
/**
* 1-based line number
*/
start: number
/**
* 1-based line number
*/
end: number
}

/**
* Represents a location in a file.
* It could be a 1-based line number, a line range, a position or a position range.
*/
export type Location = number | LineRange | Position | PositionRange

export interface FileContext {
kind: 'file'
range: LineRange
Expand Down Expand Up @@ -44,6 +87,96 @@ export interface NavigateOpts {
openInEditor?: boolean
}

/**
* Represents a filepath to identify a file.
*/
export type Filepath = FilepathInGitRepository | FilepathUri

/**
* This is used for files in a Git repository, and should be used in priority.
*/
export interface FilepathInGitRepository {
kind: 'git'

/**
* A string that is a relative path in the repository.
*/
filepath: string

/**
* A URL used to identify the Git repository in both the client and server.
* The URL should be valid for use as a git remote URL, for example:
* 1. 'https://github.com/TabbyML/tabby'
* 2. 'git://github.com/TabbyML/tabby.git'
*/
gitUrl: string

/**
* An optional Git revision which the file is at.
*/
revision?: string
}

/**
* This is used for files not in a Git repository.
*/
export interface FilepathUri {
kind: 'uri'

/**
* A string that can be parsed as a URI, used to identify the file in the client.
* The scheme of the URI could be:
* - 'untitled' means a new file not saved.
* - 'file', 'vscode-vfs' or some other protocol to access the file.
*/
uri: string
}

/**
* Represents a file and a location in it.
*/
export interface FileLocation {
/**
* The filepath of the file.
*/
filepath: Filepath

/**
* The location in the file.
* It could be a 1-based line number, a line range, a position or a position range.
*/
location: Location
}

/**
* Represents a hint to help find a symbol.
*/
export interface LookupSymbolHint {
/**
* The filepath of the file to search the symbol.
*/
filepath?: Filepath

/**
* The location in the file to search the symbol.
*/
location?: Location
}

/**
* Includes information about a symbol returned by the {@link ClientApiMethods.lookupSymbol} method.
*/
export interface SymbolInfo {
/**
* Where the symbol is found.
*/
source: FileLocation
/**
* The target location to navigate to when the symbol is clicked.
*/
target: FileLocation
}

export interface ServerApi {
init: (request: InitRequest) => void
sendMessage: (message: ChatMessage) => void
Expand All @@ -53,14 +186,7 @@ export interface ServerApi {
updateTheme: (style: string, themeClass: string) => void
updateActiveSelection: (context: Context | null) => void
}
export interface SymbolInfo {
sourceFile: string
sourceLine: number
sourceCol: number
targetFile: string
targetLine: number
targetCol: number
}

export interface ClientApiMethods {
navigate: (context: Context, opts?: NavigateOpts) => void
refresh: () => Promise<void>
Expand All @@ -84,8 +210,20 @@ export interface ClientApiMethods {

onKeyboardEvent: (type: 'keydown' | 'keyup' | 'keypress', event: KeyboardEventInit) => void

// find symbol definition location by hint filepaths and keyword
onLookupSymbol?: (hintFilepaths: string[], keyword: string) => Promise<SymbolInfo | undefined>
/**
* Find the target symbol and return the symbol information.
* @param symbol The symbol to find.
* @param hints The optional {@link LookupSymbolHint} list to help find the symbol. The hints should be sorted by priority.
* @returns The symbol information if found, otherwise undefined.
*/
lookupSymbol?: (symbol: string, hints?: LookupSymbolHint[] | undefined) => Promise<SymbolInfo | undefined>

/**
* Open the target file location in the editor.
* @param target The target file location to open.
* @returns Whether the file location is opened successfully.
*/
openInEditor: (target: FileLocation) => Promise<boolean>
}

export interface ClientApi extends ClientApiMethods {
Expand All @@ -94,17 +232,6 @@ export interface ClientApi extends ClientApiMethods {
hasCapability: (method: keyof ClientApiMethods) => Promise<boolean>
}

export const clientApiKeys: (keyof ClientApiMethods)[] = [
'navigate',
'refresh',
'onSubmitMessage',
'onApplyInEditor',
'onApplyInEditorV2',
'onLoaded',
'onCopy',
'onKeyboardEvent',
]

export interface ChatMessage {
message: string

Expand All @@ -125,10 +252,12 @@ export function createClient(target: HTMLIFrameElement, api: ClientApiMethods):
refresh: api.refresh,
onSubmitMessage: api.onSubmitMessage,
onApplyInEditor: api.onApplyInEditor,
onApplyInEditorV2: api.onApplyInEditorV2,
onLoaded: api.onLoaded,
onCopy: api.onCopy,
onKeyboardEvent: api.onKeyboardEvent,
onLookupSymbol: api.onLookupSymbol,
lookupSymbol: api.lookupSymbol,
openInEditor: api.openInEditor,
},
})
}
Expand Down
Loading

0 comments on commit fbd2de5

Please sign in to comment.