Skip to content

Commit a08b354

Browse files
committed
Add isValidExpansion utility and refactor selection logic
1 parent c736be0 commit a08b354

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

src/finders/line.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode';
22
import { SelectionCandidate } from '../types';
3+
import { isValidExpansion } from './util';
34

45
/**
56
* Finds line expansion candidate that spans from start of line containing startIndex
@@ -42,7 +43,7 @@ export function findLineExpansion(
4243
const trimmedEnd = endLineEnd - trailingWhitespace;
4344

4445
// Only return if it would expand the selection (not if selection already matches exactly)
45-
if (startIndex === trimmedStart && endIndex === trimmedEnd) {
46+
if (!isValidExpansion(startIndex, endIndex, trimmedStart, trimmedEnd)) {
4647
return null;
4748
}
4849

src/finders/quote.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SelectionCandidate } from '../types';
2+
import { isValidExpansion } from './util';
23

34
/**
45
* Finds all quote pairs (", ', `) and returns the nearest containing candidate
@@ -33,9 +34,12 @@ export function findNearestQuotePair(
3334

3435
// Check if this candidate contains the current selection
3536
if (
36-
startIndex >= candidate.start &&
37-
endIndex <= candidate.end &&
38-
!(startIndex === candidate.start && endIndex === candidate.end)
37+
isValidExpansion(
38+
startIndex,
39+
endIndex,
40+
candidate.start,
41+
candidate.end,
42+
)
3943
) {
4044
// Keep track of the smallest containing candidate
4145
if (candidate.size < smallestSize) {

src/finders/scope.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SelectionCandidate } from '../types';
2+
import { isValidExpansion } from './util';
23

34
/**
45
* Finds all balanced scopes ([], {}, (), <>) and returns the nearest containing candidate
@@ -36,9 +37,7 @@ export function findNearestScope(
3637

3738
// Check if this candidate contains the current selection
3839
if (
39-
startIndex >= candidate.start &&
40-
endIndex <= candidate.end &&
41-
!(startIndex === candidate.start && endIndex === candidate.end)
40+
isValidExpansion(startIndex, endIndex, candidate.start, candidate.end)
4241
) {
4342
// Keep track of the smallest containing candidate
4443
if (candidate.size < smallestSize) {

src/finders/token.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode';
22
import { SelectionCandidate } from '../types';
3+
import { isValidExpansion } from './util';
34

45
/**
56
* Finds word boundaries using different regex patterns for extended token detection
@@ -20,7 +21,11 @@ export function findToken(
2021

2122
// Define different word patterns to try
2223
const patterns = [
24+
/[A-Z]?[a-z]+/, // With single word characters
25+
/[a-zA-Z]+/, // With letters only
26+
/[a-zA-Z0-9]+/, // With digits
2327
/[a-zA-Z0-9_]+/, // With underscores
28+
/[a-zA-Z0-9_\-]+/, // Extended: includes underscores, dots, hyphens
2429
/[a-zA-Z0-9_\.\-]+/, // Extended: includes underscores, dots, hyphens
2530
];
2631

@@ -31,10 +36,7 @@ export function findToken(
3136
const wordEnd = document.offsetAt(wordRange.end);
3237

3338
// Check if this would be a valid expansion
34-
if (
35-
(startIndex > wordStart || endIndex < wordEnd) &&
36-
!(startIndex === wordStart && endIndex === wordEnd)
37-
) {
39+
if (isValidExpansion(startIndex, endIndex, wordStart, wordEnd)) {
3840
return {
3941
start: wordStart,
4042
end: wordEnd,

src/finders/util.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function isValidExpansion(
2+
startIndex: number,
3+
endIndex: number,
4+
rangeStart: number,
5+
rangeEnd: number,
6+
): boolean {
7+
return (
8+
startIndex >= rangeStart &&
9+
endIndex <= rangeEnd &&
10+
!(startIndex === rangeStart && endIndex === rangeEnd)
11+
);
12+
}

0 commit comments

Comments
 (0)