Skip to content

Add support for <script setup lang=ts generic="..."> #182

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

Merged
merged 9 commits into from
May 14, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update
  • Loading branch information
ota-meshi committed May 11, 2023
commit bf9d14a468e10d628d25ea01890a4df40ff67f4f
58 changes: 2 additions & 56 deletions src/script/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,8 @@ function referencesToThrough(references: Reference[], baseScope: Scope) {
* Add all references to array
*/
function addAllReferences(list: Reference[], elements: Reference[]): void {
addElementsToSortedArray(
list,
elements,
(a, b) => a.identifier.range![0] - b.identifier.range![0],
)
list.push(...elements)
list.sort((a, b) => a.identifier.range![0] - b.identifier.range![0])
}

/** Remove reference */
Expand Down Expand Up @@ -238,54 +235,3 @@ function removeScope(scopeManager: ScopeManager, scope: Scope): void {
scopeManager.scopes.splice(index, 1)
}
}

/**
* Add element to a sorted array
*/
function addElementsToSortedArray<T>(
array: T[],
elements: T[],
compare: (a: T, b: T) => number,
): void {
if (!elements.length) {
return
}
let last = elements[0]
let index = sortedLastIndex(array, (target) => compare(target, last))
for (const element of elements) {
if (compare(last, element) > 0) {
index = sortedLastIndex(array, (target) => compare(target, element))
}
let e = array[index]
while (e && compare(e, element) <= 0) {
e = array[++index]
}
array.splice(index, 0, element)
last = element
}
}

/**
* Uses a binary search to determine the highest index at which value should be inserted into array in order to maintain its sort order.
*/
function sortedLastIndex<T>(
array: T[],
compare: (target: T) => number,
): number {
let lower = 0
let upper = array.length

while (lower < upper) {
const mid = Math.floor(lower + (upper - lower) / 2)
const target = compare(array[mid])
if (target < 0) {
lower = mid + 1
} else if (target > 0) {
upper = mid
} else {
return mid + 1
}
}

return upper
}