Merge branch 'tstmove' into develop

This commit is contained in:
Vsevolod Chernetskyi 2025-03-03 16:45:38 +02:00
commit f3c34d22ba
4 changed files with 72 additions and 158 deletions

View file

@ -38,10 +38,7 @@ import { PreferenceCompletionSource } from "@src/completions/Preferences"
import { RssCompletionSource } from "@src/completions/Rss"
import { SessionsCompletionSource } from "@src/completions/Sessions"
import { SettingsCompletionSource } from "@src/completions/Settings"
import {
LinearBufferCompletionSource,
BufferTreeCompletionSource,
} from "@src/completions/Tab"
import { BufferCompletionSource } from "@src/completions/Tab"
import { TabAllCompletionSource } from "@src/completions/TabAll"
import { ThemeCompletionSource } from "@src/completions/Theme"
import { TabHistoryCompletionSource } from "@src/completions/TabHistory"
@ -133,8 +130,7 @@ export function enableCompletions() {
BmarkCompletionSource,
BookmarkFolderCompletionSource,
TabAllCompletionSource,
LinearBufferCompletionSource,
BufferTreeCompletionSource,
BufferCompletionSource,
ExcmdCompletionSource,
ThemeCompletionSource,
TabHistoryCompletionSource,

View file

@ -18,7 +18,6 @@ class BufferCompletionOption
public isAlternative = false,
container: browser.contextualIdentities.ContextualIdentity,
public tabIndex: number,
titlePrefix: number,
) {
super()
@ -31,6 +30,7 @@ class BufferCompletionOption
if (tab.active) preplain += "%"
else if (isAlternative) {
preplain += "#"
this.value = "#"
}
let pre = preplain
if (tab.pinned) preplain += "P"
@ -52,7 +52,7 @@ class BufferCompletionOption
this.fuseKeys.push(preplain)
// Push properties we want to fuzmatch on
this.fuseKeys.push(String(titlePrefix), tab.title, tab.url)
this.fuseKeys.push(String(tab.index + 1), tab.title, tab.url)
// Create HTMLElement
const favIconUrl = tab.favIconUrl
@ -66,7 +66,9 @@ class BufferCompletionOption
<td class="prefixplain" hidden>${preplain}</td>
<td class="container"></td>
<td class="icon"><img loading="lazy" src="${favIconUrl}" /></td>
<td class="title">${titlePrefix}: ${indicator} ${tab.title}</td>
<td class="title">
${this.tabIndex + 1}: ${indicator} ${tab.title}
</td>
<td class="content">
<a class="url" target="_blank" href=${tab.url}>${tab.url}</a>
</td>
@ -74,7 +76,7 @@ class BufferCompletionOption
}
}
abstract class BufferCompletionSource extends Completions.CompletionSourceFuse {
export class BufferCompletionSource extends Completions.CompletionSourceFuse {
public options: BufferCompletionOption[]
private shouldSetStateFromScore = true
@ -83,13 +85,29 @@ abstract class BufferCompletionSource extends Completions.CompletionSourceFuse {
// callback faffery
// - sort out the element redrawing.
constructor(_parent, prefixes: string[], className: string) {
super(prefixes, className, "Tabs")
constructor(private _parent) {
super(
[
"tab",
"tabclose",
"tabdetach",
"tabduplicate",
"tabmove",
"tabrename",
"tabdiscard",
"pin",
"tstmove",
"tstmoveafter",
"tstattach",
],
"BufferCompletionSource",
"Tabs",
)
this.sortScoredOptions = true
this.shouldSetStateFromScore =
config.get("completions", "Tab", "autoselect") === "true"
this.updateOptions()
_parent.appendChild(this.node)
this._parent.appendChild(this.node)
Messaging.addListener("tab_changes", () => this.reactToTabChanges())
}
@ -118,14 +136,26 @@ abstract class BufferCompletionSource extends Completions.CompletionSourceFuse {
): Completions.ScoredOption[] {
const args = query.trim().split(/\s+/gu)
if (args.length === 1) {
const arg = args[0]
if (arg === "#") {
return this.optionsLike(option => option.isAlternative, options)
}
const searchId = Number(arg)
if (Number.isInteger(searchId)) {
return this.optionsBySearchId(searchId, options)
// if query is an integer n and |n| < options.length
if (Number.isInteger(Number(args[0]))) {
let index = Number(args[0]) - 1
if (Math.abs(index) < options.length) {
index = index.mod(options.length)
// options order might change by scored sorting
return this.TabscoredOptionsStartsWithN(index, options)
}
} else if (args[0] === "#") {
for (const [index, option] of enumerate(options)) {
if (option.isAlternative) {
return [
{
index,
option,
score: 0,
},
]
}
}
}
}
@ -133,21 +163,26 @@ abstract class BufferCompletionSource extends Completions.CompletionSourceFuse {
return super.scoredOptions(query)
}
protected optionsLike(
predicate: (o: BufferCompletionOption) => boolean,
/** Return the scoredOption[] result for the tab index startswith n */
private TabscoredOptionsStartsWithN(
n: number,
options: BufferCompletionOption[],
): Completions.ScoredOption[] {
const result = []
const nstr = (n + 1).toString()
const res = []
for (const [index, option] of enumerate(options)) {
if (predicate(option)) {
result.push({
index,
if ((option.tabIndex + 1).toString().startsWith(nstr)) {
res.push({
index, // index is not tabIndex, changed by score
option,
score: 0,
})
}
}
return result
// old input will change order: 12 => 123 => 12
res.sort((a, b) => a.option.tabIndex - b.option.tabIndex)
return res
}
private async fillOptions(prefix: string) {
@ -173,16 +208,13 @@ abstract class BufferCompletionSource extends Completions.CompletionSourceFuse {
if (!tab_container) {
tab_container = Containers.DefaultContainer
}
const isAlternative = tab.index === altTab.index
const titlePrefix = this.titlePrefix(index, tab)
options.push(
new BufferCompletionOption(
this.completionValue(titlePrefix, isAlternative),
(index + 1).toString(),
tab,
isAlternative,
tab.index === altTab.index,
tab_container,
index,
titlePrefix,
),
)
}
@ -266,119 +298,4 @@ abstract class BufferCompletionSource extends Completions.CompletionSourceFuse {
}
return this.options[option.tabIndex]
}
/**
* Provide identifier, which will be used before tab title in the completion option.
* @param index of the tab calculated by Tridactyl
* @param tab Tab
*/
protected abstract titlePrefix(index: number, tab: browser.tabs.Tab): number
/**
* Provide value, which will be used on tab completion (i.e. when user selects tab option using <Space> or <Enter>).
* @param titlePrefix identifier user before tab title
* @param isAlternative marker if completion value is for previous tab
*/
protected abstract completionValue(
titlePrefix: number,
isAlternative: boolean,
): string
/**
* Filter list of options by option identifier.
* @param searchId identifier of the option
* @param options list of options to search through
*/
protected abstract optionsBySearchId(
searchId: number,
options: BufferCompletionOption[],
): Completions.ScoredOption[]
}
export class LinearBufferCompletionSource extends BufferCompletionSource {
constructor(_parent) {
super(
_parent,
[
"tab",
"tabclose",
"tabdetach",
"tabduplicate",
"tabmove",
"tabrename",
"tabdiscard",
"pin",
],
"LinearBufferCompletionSource",
)
}
protected titlePrefix(index: number, _tab: browser.tabs.Tab): number {
return index + 1
}
protected completionValue(
titlePrefix: number,
isAlternative: boolean,
): string {
if (isAlternative) {
return "#"
}
return String(titlePrefix)
}
protected optionsBySearchId(
searchId: number,
options: BufferCompletionOption[],
): Completions.ScoredOption[] {
const index = (searchId - 1).mod(options.length)
options.sort((a, b) => a.tabIndex - b.tabIndex)
return this.tabScoredOptionsStartsWithN(index, options)
}
/** Return the scoredOption[] result for the tab index startswith n */
private tabScoredOptionsStartsWithN(
n: number,
options: BufferCompletionOption[],
): Completions.ScoredOption[] {
const nstr = (n + 1).toString()
return this.optionsLike(
option => (option.tabIndex + 1).toString().startsWith(nstr),
options,
)
}
}
/**
* TST specifics for tab completion.
*
* At the moment the only difference to linear tabs is that TST source uses tab
* ID in place of tab index for identification.
*/
export class BufferTreeCompletionSource extends BufferCompletionSource {
constructor(_parent) {
super(
_parent,
["tstmove", "tstmoveafter", "tstattach"],
"BufferTreeCompletionSource",
)
}
protected titlePrefix(_index: number, tab: browser.tabs.Tab): number {
return tab.id
}
protected completionValue(
titlePrefix: number,
_isAlternative: boolean,
): string {
return String(titlePrefix)
}
protected optionsBySearchId(
searchId: number,
options: BufferCompletionOption[],
): Completions.ScoredOption[] {
return this.optionsLike(option => option.tabId === searchId, options)
}
}

View file

@ -6214,22 +6214,25 @@ export async function elementunhide() {
* Move the current TST tree to be just in front of the tab specified.
*/
//#background
export async function tstmove(tabId: string) {
treestyletab.moveTreeBefore(Number(tabId))
export async function tstmove(index: string) {
const tabId = await idFromIndex(index)
treestyletab.moveTreeBefore(tabId)
}
/**
* Move the current TST tree to be right after the tab specified.
*/
//#background
export async function tstmoveafter(tabId: string) {
treestyletab.moveTreeAfter(Number(tabId))
export async function tstmoveafter(index: string) {
const tabId = await idFromIndex(index)
treestyletab.moveTreeAfter(tabId)
}
/**
* Attach current tree as a child to the selected parent.
*/
//#background
export async function tstattach(parentTabId: string) {
treestyletab.attachTree(Number(parentTabId))
export async function tstattach(index: string) {
const tabId = await idFromIndex(index)
treestyletab.attachTree(tabId)
}

View file

@ -1,5 +1,5 @@
import * as ExtensionInfo from "@src/lib/extension_info"
import { browserBg } from "@src/lib/webext"
import { activeTab } from "@src/lib/webext"
export async function moveTreeBefore(tabId: number) {
await ExtensionInfo.messageExtension("tree_style_tab", {
@ -20,9 +20,7 @@ export async function moveTreeAfter(tabId: number) {
}
export async function attachTree(parentTabId: number) {
const currentTab = (
await browserBg.tabs.query({ currentWindow: true, active: true })
)[0]
const currentTab = await activeTab()
await ExtensionInfo.messageExtension("tree_style_tab", {
type: "attach",
child: currentTab.id,