Skip to content
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

Strict null checking snippetsService.ts #63456

Merged
merged 1 commit into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/tsconfig.strictNullChecks.json
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@
"./vs/workbench/parts/snippets/electron-browser/snippetCompletionProvider.ts",
"./vs/workbench/parts/snippets/electron-browser/snippets.contribution.ts",
"./vs/workbench/parts/snippets/electron-browser/snippetsFile.ts",
"./vs/workbench/parts/snippets/electron-browser/snippetsService.ts",
"./vs/workbench/parts/surveys/electron-browser/nps.contribution.ts",
"./vs/workbench/parts/tasks/common/problemCollectors.ts",
"./vs/workbench/parts/tasks/common/problemMatcher.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ export class SnippetFile {
constructor(
readonly source: SnippetSource,
readonly location: URI,
readonly defaultScopes: string[],
private readonly _extension: IExtensionDescription,
public defaultScopes: string[] | undefined,
private readonly _extension: IExtensionDescription | undefined,
private readonly _fileService: IFileService
) {
this.isGlobalSnippets = extname(location.path) === '.code-snippets';
Expand Down
48 changes: 30 additions & 18 deletions src/vs/workbench/parts/snippets/electron-browser/snippetsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace snippetExt {
location: URI;
}

export function toValidSnippet(extension: IExtensionPointUser<ISnippetsExtensionPoint[]>, snippet: ISnippetsExtensionPoint, modeService: IModeService): IValidSnippetsExtensionPoint {
export function toValidSnippet(extension: IExtensionPointUser<ISnippetsExtensionPoint[]>, snippet: ISnippetsExtensionPoint, modeService: IModeService): IValidSnippetsExtensionPoint | null {

if (isFalsyOrWhitespace(snippet.path)) {
extension.collector.error(localize(
Expand Down Expand Up @@ -167,28 +167,35 @@ class SnippetsService implements ISnippetsService {

getSnippets(languageId: LanguageId): Promise<Snippet[]> {
return this._joinSnippets().then(() => {
const langName = this._modeService.getLanguageIdentifier(languageId).language;
const result: Snippet[] = [];
const promises: Promise<any>[] = [];
this._files.forEach(file => {
promises.push(file.load()
.then(file => file.select(langName, result))
.catch(err => this._logService.error(err, file.location.toString()))
);
});

const languageIdentifier = this._modeService.getLanguageIdentifier(languageId);
if (languageIdentifier) {
const langName = languageIdentifier.language;
this._files.forEach(file => {
promises.push(file.load()
.then(file => file.select(langName, result))
.catch(err => this._logService.error(err, file.location.toString()))
);
});
}
return Promise.all(promises).then(() => result);
});
}

getSnippetsSync(languageId: LanguageId): Snippet[] {
const langName = this._modeService.getLanguageIdentifier(languageId).language;
const result: Snippet[] = [];
this._files.forEach(file => {
// kick off loading (which is a noop in case it's already loaded)
// and optimistically collect snippets
file.load().catch(err => { /*ignore*/ });
file.select(langName, result);
});
const languageIdentifier = this._modeService.getLanguageIdentifier(languageId);
if (languageIdentifier) {
const langName = languageIdentifier.language;
this._files.forEach(file => {
// kick off loading (which is a noop in case it's already loaded)
// and optimistically collect snippets
file.load().catch(err => { /*ignore*/ });
file.select(langName, result);
});
}
return result;
}

Expand All @@ -203,9 +210,14 @@ class SnippetsService implements ISnippetsService {
continue;
}

if (this._files.has(validContribution.location.toString())) {
this._files.get(validContribution.location.toString()).defaultScopes.push(validContribution.language);

const resource = validContribution.location.toString();
if (this._files.has(resource)) {
const file = this._files.get(resource);
if (file.defaultScopes) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the one change I was unsure about. Not sure what we are supposed to do if we get here and file.defaultScopes is undefined. Don't do anything? Create a new array?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, a new array should be created. On the other hand this is a rare corner case and only happens when different snippet contributions point to the same file.. We can also nuke that if and always let the last one win...

file.defaultScopes.push(validContribution.language);
} else {
file.defaultScopes = [];
}
} else {
const file = new SnippetFile(SnippetSource.Extension, validContribution.location, validContribution.language ? [validContribution.language] : undefined, extension.description, this._fileService);
this._files.set(file.location.toString(), file);
Expand Down