-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge v0.4.1 'dev' into 'master' (#30)
* Better support for partial completions (#29) * Bugfix for Issue #28 * Updated CHANGELOG for v0.4.1
- Loading branch information
Showing
6 changed files
with
196 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2018 Tektronix Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict' | ||
|
||
import { CompletionItem } from 'vscode-languageserver' | ||
|
||
export function isPartialMatch(content: string, completion: CompletionItem): boolean { | ||
// If content is an empty string, then everything is a partial match | ||
if (content.localeCompare('') === 0) { | ||
return true | ||
} | ||
|
||
const completionData: Array<string> = (completion.data === undefined) ? [] : completion.data | ||
|
||
let names: Array<string> = content.split('.') | ||
|
||
// Get the (possibly partial or empty) name requesting suggestions. | ||
// Array.pop returns undefined if the array is empty but String.split always returns an | ||
// array with at least 1 item, so disregard the undefined type. | ||
const lastName = names.pop() as string | ||
|
||
// Reverse the remaining names so we can more easily match against CompletionItem.data. | ||
names = names.reverse() | ||
|
||
// If the given completion's namespace length does not match our content's namespace length | ||
if (completionData.length !== names.length) { | ||
return false | ||
} | ||
|
||
// If the given completion has an identical namespace | ||
if (completionData.join('.').localeCompare(names.join('.')) === 0) { | ||
// If the last name is an empty string, then everything is a partial match | ||
if (lastName.localeCompare('') === 0) { | ||
return true | ||
} | ||
|
||
const labelRegexp = new RegExp('^' + lastName + '.*$') | ||
|
||
const matches = completion.label.match(labelRegexp) | ||
|
||
if (matches === null) { | ||
return false | ||
} | ||
else { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2018 Tektronix Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
// tslint:disable:no-implicit-dependencies no-magic-numbers prefer-function-over-method | ||
import { assert } from 'chai' | ||
import { suite, test } from 'mocha-typescript' | ||
import { CompletionItem } from 'vscode-languageclient' | ||
|
||
import { isPartialMatch } from '../../../server/src/completionProcessor' | ||
|
||
function toString(completion: CompletionItem): string { | ||
let result = '{label: "' + completion.label + '"' | ||
|
||
if (completion.data !== undefined) { | ||
result += ', data: [' | ||
completion.data.forEach((item: string) => { | ||
result += '"' + item + '", ' | ||
}) | ||
// Remove the trailing ", " | ||
result = result.slice(0, result.length - 2) | ||
result += ']' | ||
} | ||
|
||
return result + '}' | ||
} | ||
|
||
@suite class CompletionProcessorTest { | ||
@test('isPartialMatch') | ||
isPartialMatchTest(): void { | ||
let testString = '' | ||
let testCompletion: CompletionItem = { | ||
label: 'a' | ||
} | ||
assert( | ||
isPartialMatch(testString, testCompletion), | ||
'"' + testString + '" did not partially match "' + toString(testCompletion) + '"' | ||
) | ||
|
||
testCompletion = { | ||
data: ['b'], | ||
label: 'a' | ||
} | ||
assert( | ||
isPartialMatch(testString, testCompletion), | ||
'"' + testString + '" did not partially match "' + toString(testCompletion) + '"' | ||
) | ||
|
||
testString = 'a.' | ||
testCompletion = { | ||
label: 'a' | ||
} | ||
assert( | ||
! isPartialMatch(testString, testCompletion), | ||
'"' + testString + '" partially matched "' + toString(testCompletion) + '"' | ||
) | ||
|
||
testCompletion = { | ||
data: ['a'], | ||
label: 'b' | ||
} | ||
assert( | ||
isPartialMatch(testString, testCompletion), | ||
'"' + testString + '" did not partially match "' + toString(testCompletion) + '"' | ||
) | ||
|
||
testString = 'a.p' | ||
testCompletion = { | ||
label: 'a' | ||
} | ||
assert( | ||
! isPartialMatch(testString, testCompletion), | ||
'"' + testString + '" partially matched "' + toString(testCompletion) + '"' | ||
) | ||
|
||
testCompletion = { | ||
data: ['a'], | ||
label: 'b' | ||
} | ||
assert( | ||
! isPartialMatch(testString, testCompletion), | ||
'"' + testString + '" partially matched "' + toString(testCompletion) + '"' | ||
) | ||
|
||
testCompletion = { | ||
data: ['b'], | ||
label: 'partial' | ||
} | ||
assert( | ||
! isPartialMatch(testString, testCompletion), | ||
'"' + testString + '" partially matched "' + toString(testCompletion) + '"' | ||
) | ||
|
||
testCompletion = { | ||
data: ['a'], | ||
label: 'partial' | ||
} | ||
assert( | ||
isPartialMatch(testString, testCompletion), | ||
'"' + testString + '" did not partially match "' + toString(testCompletion) + '"' | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters