MCPcopy Create free account
hub / github.com/nodejs/node / matchSegment

Function matchSegment

test/fixtures/snapshot/typescript.js:142507–142562  ·  view source on GitHub ↗
(candidate, segment, stringToWordSpans)

Source from the content-addressed store, hash-verified

142505 }
142506 }
142507 function matchSegment(candidate, segment, stringToWordSpans) {
142508 // First check if the segment matches as is. This is also useful if the segment contains
142509 // characters we would normally strip when splitting into parts that we also may want to
142510 // match in the candidate. For example if the segment is "@int" and the candidate is
142511 // "@int", then that will show up as an exact match here.
142512 //
142513 // Note: if the segment contains a space or an asterisk then we must assume that it's a
142514 // multi-word segment.
142515 if (every(segment.totalTextChunk.text, function (ch) { return ch !== 32 /* CharacterCodes.space */ && ch !== 42 /* CharacterCodes.asterisk */; })) {
142516 var match = matchTextChunk(candidate, segment.totalTextChunk, stringToWordSpans);
142517 if (match)
142518 return match;
142519 }
142520 // The logic for pattern matching is now as follows:
142521 //
142522 // 1) Break the segment passed in into words. Breaking is rather simple and a
142523 // good way to think about it that if gives you all the individual alphanumeric words
142524 // of the pattern.
142525 //
142526 // 2) For each word try to match the word against the candidate value.
142527 //
142528 // 3) Matching is as follows:
142529 //
142530 // a) Check if the word is a prefix of the candidate, in a case insensitive or
142531 // sensitive manner. If it does, return that there was an exact match if the word and candidate are the same length, else a prefix match.
142532 //
142533 // If the word is entirely lowercase:
142534 // b) Then check if it is contained anywhere in the
142535 // candidate in a case insensitive manner. If so, return that there was a substring
142536 // match.
142537 //
142538 // Note: We only have a substring match if the lowercase part is prefix match of
142539 // some word part. That way we don't match something like 'Class' when the user
142540 // types 'a'. But we would match 'FooAttribute' (since 'Attribute' starts with
142541 // 'a').
142542 //
142543 // c) The word is all lower case. Is it a case insensitive substring of the candidate starting
142544 // on a part boundary of the candidate?
142545 //
142546 // Else:
142547 // d) If the word was not entirely lowercase, then check if it is contained in the
142548 // candidate in a case *sensitive* manner. If so, return that there was a substring
142549 // match.
142550 //
142551 // e) If the word was not entirely lowercase, then attempt a camel cased match as
142552 // well.
142553 //
142554 // Only if all words have some sort of match is the pattern considered matched.
142555 var subWordTextChunks = segment.subWordTextChunks;
142556 var bestMatch;
142557 for (var _i = 0, subWordTextChunks_1 = subWordTextChunks; _i < subWordTextChunks_1.length; _i++) {
142558 var subWordTextChunk = subWordTextChunks_1[_i];
142559 bestMatch = betterMatch(bestMatch, matchTextChunk(candidate, subWordTextChunk, stringToWordSpans));
142560 }
142561 return bestMatch;
142562 }
142563 function betterMatch(a, b) {
142564 return ts.min(a, b, compareMatches);

Callers 2

createPatternMatcherFunction · 0.85
getFullMatchFunction · 0.85

Calls 3

matchTextChunkFunction · 0.85
betterMatchFunction · 0.85
everyFunction · 0.70

Tested by

no test coverage detected