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

Low-hanging nullable fruit #5186

Merged
merged 19 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
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
8 changes: 1 addition & 7 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ export function safeLength<T>(arr: T[] | undefined) {
return arr ? arr.length : 0;
}

export async function buildPromiseChain<T, TResult>(array: T[], builder: (item: T) => Promise<TResult>): Promise<TResult> {
return array.reduce(
async (promise, n) => promise.then(async () => builder(n)),
Promise.resolve<TResult>(null));
}

export async function execChildProcess(command: string, workingDirectory: string = getExtensionPath()): Promise<string> {
return new Promise<string>((resolve, reject) => {
cp.exec(command, { cwd: workingDirectory, maxBuffer: 500 * 1024 }, (error, stdout, stderr) => {
Expand Down Expand Up @@ -229,4 +223,4 @@ export function isSubfolderOf(subfolder: string, folder: string): boolean {
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/features/diagnosticsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class DiagnosticsProvider extends AbstractSupport {

this._subscriptions.push(this._validateCurrentDocumentPipe
.pipe(debounceTime(750))
.subscribe(x => this._validateDocument(x)));
.subscribe(async document => await this._validateDocument(document)));

this._subscriptions.push(this._validateAllPipe
.pipe(debounceTime(3000))
Expand Down
5 changes: 5 additions & 0 deletions src/features/fileOpenCloseProvider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { IDisposable } from "../Disposable";
import { OmniSharpServer } from "../omnisharp/server";
import * as vscode from 'vscode';
Expand Down
7 changes: 4 additions & 3 deletions src/omnisharp/LanguageMiddlewareFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ type RemapParameterType<M extends keyof RemapApi> = GetRemapType<NonNullable<Rem

export class LanguageMiddlewareFeature implements IDisposable {
private readonly _middlewares: LanguageMiddleware[];
private _registration: IDisposable;
private _registration: IDisposable | undefined;

constructor() {
this._middlewares = [];
}

public dispose(): void {
this._registration.dispose();
// this._registration should always be defined, but just in case we never register...
this._registration?.dispose();
}

public register(): void {
Expand Down Expand Up @@ -69,4 +70,4 @@ export class LanguageMiddlewareFeature implements IDisposable {
return original;
}
}
}
}
3 changes: 1 addition & 2 deletions src/omnisharp/OmnisharpDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ export class OmnisharpDownloader {
this.eventStream.post(new InstallationSuccess());
return true;
}

return false;
}
return false;
}

public async GetLatestVersion(serverUrl: string, latestVersionFileServerPath: string): Promise<string> {
Expand Down
6 changes: 3 additions & 3 deletions src/omnisharp/OmnisharpManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export class OmnisharpManager {
private platformInfo: PlatformInformation) {
}

public async GetOmniSharpLaunchInfo(defaultOmnisharpVersion: string, omnisharpPath: string, useFramework: boolean, serverUrl: string, latestVersionFileServerPath: string, installPath: string, extensionPath: string): Promise<LaunchInfo> {
if (!omnisharpPath) {
public async GetOmniSharpLaunchInfo(defaultOmnisharpVersion: string, omnisharpPath: string | undefined, useFramework: boolean, serverUrl: string, latestVersionFileServerPath: string, installPath: string, extensionPath: string): Promise<LaunchInfo> {
if (omnisharpPath === undefined) {
50Wliu marked this conversation as resolved.
Show resolved Hide resolved
// If omnisharpPath was not specified, return the default path.
let basePath = path.resolve(extensionPath, '.omnisharp', defaultOmnisharpVersion + (useFramework ? '' : `-net${modernNetVersion}`));
const basePath = path.resolve(extensionPath, '.omnisharp', defaultOmnisharpVersion + (useFramework ? '' : `-net${modernNetVersion}`));
return this.GetLaunchInfo(this.platformInfo, useFramework, basePath);
}

Expand Down
4 changes: 2 additions & 2 deletions src/omnisharp/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function activate(context: vscode.ExtensionContext, packageJSON: an
const languageMiddlewareFeature = new LanguageMiddlewareFeature();
languageMiddlewareFeature.register();
disposables.add(languageMiddlewareFeature);
let localDisposables: CompositeDisposable;
let localDisposables: CompositeDisposable | undefined;
const testManager = new TestManager(optionProvider, server, eventStream, languageMiddlewareFeature);
const completionProvider = new CompletionProvider(server, languageMiddlewareFeature);

Expand Down Expand Up @@ -130,7 +130,7 @@ export async function activate(context: vscode.ExtensionContext, packageJSON: an
if (localDisposables) {
localDisposables.dispose();
}
localDisposables = null;
localDisposables = undefined;
}));

disposables.add(registerCommands(context, server, platformInfo, eventStream, optionProvider, omnisharpMonoResolver, packageJSON, extensionPath));
Expand Down
4 changes: 2 additions & 2 deletions src/omnisharp/fileOperationsResponseEditBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { toRange2 } from './typeConversion';
export async function buildEditForResponse(changes: FileOperationResponse[], languageMiddlewareFeature: LanguageMiddlewareFeature, token: vscode.CancellationToken): Promise<boolean> {
let edit = new vscode.WorkspaceEdit();

let fileToOpen: Uri = null;
let fileToOpen: Uri | undefined;

if (!changes || !Array.isArray(changes) || !changes.length) {
return true;
Expand Down Expand Up @@ -55,7 +55,7 @@ export async function buildEditForResponse(changes: FileOperationResponse[], lan
// and replaced with a command that can only close the active editor.
// If files were renamed that weren't the active editor, their tabs will
// be left open and marked as "deleted" by VS Code
return fileToOpen != null
return fileToOpen !== undefined
? applyEditPromise.then(_ => {
return vscode.commands.executeCommand("vscode.open", fileToOpen);
})
Expand Down
2 changes: 1 addition & 1 deletion src/omnisharp/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function resourcesToLaunchTargets(resources: vscode.Uri[]): LaunchTarget[
let buckets: vscode.Uri[];

if (workspaceFolderToUriMap.has(folder.index)) {
buckets = workspaceFolderToUriMap.get(folder.index);
buckets = workspaceFolderToUriMap.get(folder.index)!; // Ensured valid via has.
} else {
buckets = [];
workspaceFolderToUriMap.set(folder.index, buckets);
Expand Down
2 changes: 1 addition & 1 deletion src/omnisharp/loggingEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class OmnisharpInitialisation implements BaseEvent {

export class OmnisharpLaunch implements BaseEvent {
type = EventType.OmnisharpLaunch;
constructor(public hostVersion: string, public hostPath: string, public hostIsMono: boolean, public command: string, public pid: number) { }
constructor(public hostVersion: string | undefined, public hostPath: string | undefined, public hostIsMono: boolean, public command: string, public pid: number) { }
}

export class PackageInstallStart implements BaseEvent {
Expand Down
76 changes: 34 additions & 42 deletions src/omnisharp/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { vscode, WorkspaceConfiguration } from '../vscodeAdapter';

export class Options {
constructor(
public path: string,
public path: string | undefined,
public useModernNet: boolean,
public useGlobalMono: string,
public waitForDebugger: boolean,
Expand Down Expand Up @@ -48,13 +48,13 @@ export class Options {
public inlayHintsForImplicitVariableTypes: boolean,
public inlayHintsForLambdaParameterTypes: boolean,
public inlayHintsForImplicitObjectCreation: boolean,
public razorPluginPath?: string,
public defaultLaunchSolution?: string,
public monoPath?: string,
public dotnetPath?: string,
public excludePaths?: string[],
public maxProjectFileCountForDiagnosticAnalysis?: number | null,
public testRunSettings?: string) {
public razorPluginPath: string | undefined,
public defaultLaunchSolution: string | undefined,
public monoPath: string | undefined,
public dotnetPath: string | undefined,
public excludePaths: string[],
public maxProjectFileCountForDiagnosticAnalysis: number,
public testRunSettings: string | undefined) {
}

public static Read(vscode: vscode): Options {
Expand All @@ -72,8 +72,8 @@ export class Options {
const path = Options.readPathOption(csharpConfig, omnisharpConfig);
const useModernNet = omnisharpConfig.get<boolean>("useModernNet", false);
const useGlobalMono = Options.readUseGlobalMonoOption(omnisharpConfig, csharpConfig);
const monoPath = omnisharpConfig.get<string>('monoPath', undefined) || undefined;
const dotnetPath = omnisharpConfig.get<string>('dotnetPath', undefined) || undefined;
const monoPath = omnisharpConfig.get<string>('monoPath');
const dotnetPath = omnisharpConfig.get<string>('dotnetPath');

const waitForDebugger = omnisharpConfig.get<boolean>('waitForDebugger', false);

Expand All @@ -87,7 +87,7 @@ export class Options {

const projectLoadTimeout = omnisharpConfig.get<number>('projectLoadTimeout', 60);
const maxProjectResults = omnisharpConfig.get<number>('maxProjectResults', 250);
const defaultLaunchSolution = omnisharpConfig.get<string>('defaultLaunchSolution', undefined);
const defaultLaunchSolution = omnisharpConfig.get<string>('defaultLaunchSolution');
const useEditorFormattingSettings = omnisharpConfig.get<boolean>('useEditorFormattingSettings', true);

const enableRoslynAnalyzers = omnisharpConfig.get<boolean>('enableRoslynAnalyzers', false);
Expand Down Expand Up @@ -130,13 +130,13 @@ export class Options {

const enableMsBuildLoadProjectsOnDemand = omnisharpConfig.get<boolean>('enableMsBuildLoadProjectsOnDemand', false);

const razorDisabled = !!razorConfig && razorConfig.get<boolean>('disabled', false);
const razorDevMode = !!razorConfig && razorConfig.get<boolean>('devmode', false);
const razorPluginPath = razorConfig ? razorConfig.get<string>('plugin.path', undefined) : undefined;
const razorDisabled = razorConfig?.get<boolean>('disabled', false) ?? false;
const razorDevMode = razorConfig?.get<boolean>('devmode', false) ?? false;
const razorPluginPath = razorConfig?.get<string>('plugin.path') ?? undefined;

const maxProjectFileCountForDiagnosticAnalysis = csharpConfig.get<number | null>('maxProjectFileCountForDiagnosticAnalysis', 1000);
const maxProjectFileCountForDiagnosticAnalysis = csharpConfig.get<number>('maxProjectFileCountForDiagnosticAnalysis', 1000);

const testRunSettings = omnisharpConfig.get<string>('testRunSettings', undefined);
const testRunSettings = omnisharpConfig.get<string>('testRunSettings');

const excludePaths = this.getExcludedPaths(vscode);

Expand Down Expand Up @@ -193,10 +193,7 @@ export class Options {
}

public static getExcludedPaths(vscode: vscode, includeSearchExcludes: boolean = false): string[] {
let workspaceConfig = vscode.workspace.getConfiguration(undefined, null);
if (!workspaceConfig) {
return [];
}
const workspaceConfig = vscode.workspace.getConfiguration();

let excludePaths = getExcludes(workspaceConfig, 'files.exclude');

Expand All @@ -207,8 +204,8 @@ export class Options {
return excludePaths;

function getExcludes(config: WorkspaceConfiguration, option: string): string[] {
let optionValue = config.get<{ [i: string]: boolean }>(option);
if (!optionValue) {
const optionValue = config.get<{ [i: string]: boolean }>(option);
if (optionValue === undefined) {
return [];
}

Expand All @@ -218,7 +215,7 @@ export class Options {
}
}

private static readPathOption(csharpConfig: WorkspaceConfiguration, omnisharpConfig: WorkspaceConfiguration): string | null {
private static readPathOption(csharpConfig: WorkspaceConfiguration, omnisharpConfig: WorkspaceConfiguration): string | undefined {
if (omnisharpConfig.has('path')) {
// If 'omnisharp.path' setting was found, use it.
return omnisharpConfig.get<string>('path');
Expand All @@ -228,32 +225,27 @@ export class Options {
return csharpConfig.get<string>('omnisharp');
}
else {
// Otherwise, null.
return null;
// Otherwise, undefined.
return undefined;
}
}

private static readUseGlobalMonoOption(omnisharpConfig: WorkspaceConfiguration, csharpConfig: WorkspaceConfiguration): string {
function toUseGlobalMonoValue(value: boolean): string {
function toUseGlobalMonoValue(value?: boolean): string | undefined {
if (value === undefined) {
return undefined;
}

// True means 'always' and false means 'auto'.
return value ? "always" : "auto";
}

if (omnisharpConfig.has('useGlobalMono')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not confident this fallback logic ever worked...I'm running the singleCsproj integration test suite right now which doesn't have useGlobalMono defined, but this still returns true (with get returning "auto", the default).

I suppose the upside is this whole section is going away soon, so I don't suppose it matters too much.

Copy link
Member

Choose a reason for hiding this comment

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

This bit of code will be removed prior to the next release to address #5120

// If 'omnisharp.useGlobalMono' setting was found, just use it.
return omnisharpConfig.get<string>('useGlobalMono', "auto");
}
else if (omnisharpConfig.has('useMono')) {
// BACKCOMPAT: If 'omnisharp.useMono' setting was found, true maps to "always" and false maps to "auto"
return toUseGlobalMonoValue(omnisharpConfig.get<boolean>('useMono'));
}
else if (csharpConfig.has('omnisharpUsesMono')) {
// BACKCOMPAT: If 'csharp.omnisharpUsesMono' setting was found, true maps to "always" and false maps to "auto"
return toUseGlobalMonoValue(csharpConfig.get<boolean>('omnisharpUsesMono'));
}
else {
// Otherwise, the default value is "auto".
return "auto";
}
// If 'omnisharp.useGlobalMono' is set, use that.
// Otherwise, for backcompat, look for 'omnisharp.useMono' and 'csharp.omnisharpUsesMono'.
// If both of those aren't found, return 'auto' as the default value.
return omnisharpConfig.get<string>('useGlobalMono') ??
toUseGlobalMonoValue(omnisharpConfig.get<boolean>('useMono')) ??
toUseGlobalMonoValue(csharpConfig.get<boolean>('omnisharpUsesMono')) ??
"auto";
}
}
18 changes: 9 additions & 9 deletions src/omnisharp/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,36 +957,36 @@ export namespace V2 {
}
}

export function findNetFrameworkTargetFramework(project: MSBuildProject): TargetFramework {
let regexp = new RegExp('^net[1-4]');
export function findNetFrameworkTargetFramework(project: MSBuildProject): TargetFramework | undefined {
const regexp = new RegExp('^net[1-4]');
return project.TargetFrameworks.find(tf => regexp.test(tf.ShortName));
}

export function findNetCoreTargetFramework(project: MSBuildProject): TargetFramework {
export function findNetCoreTargetFramework(project: MSBuildProject): TargetFramework | undefined {
return findNetCoreAppTargetFramework(project) ?? findModernNetFrameworkTargetFramework(project);
}

export function findNetCoreAppTargetFramework(project: MSBuildProject): TargetFramework {
export function findNetCoreAppTargetFramework(project: MSBuildProject): TargetFramework | undefined {
return project.TargetFrameworks.find(tf => tf.ShortName.startsWith('netcoreapp'));
}

export function findModernNetFrameworkTargetFramework(project: MSBuildProject): TargetFramework {
let regexp = new RegExp('^net[5-9]');
export function findModernNetFrameworkTargetFramework(project: MSBuildProject): TargetFramework | undefined {
const regexp = new RegExp('^net[5-9]');
const targetFramework = project.TargetFrameworks.find(tf => regexp.test(tf.ShortName));

// Shortname is being reported as net50 instead of net5.0
if (targetFramework !== undefined && targetFramework.ShortName.charAt(4) !== ".") {
targetFramework.ShortName = targetFramework.ShortName.substr(0, 4) + "." + targetFramework.ShortName.substr(4);
targetFramework.ShortName = `${targetFramework.ShortName.substring(0, 4)}.${targetFramework.ShortName.substring(4)}`;
}

return targetFramework;
}

export function findNetStandardTargetFramework(project: MSBuildProject): TargetFramework {
export function findNetStandardTargetFramework(project: MSBuildProject): TargetFramework | undefined {
return project.TargetFrameworks.find(tf => tf.ShortName.startsWith('netstandard'));
}

export function isDotNetCoreProject(project: MSBuildProject): Boolean {
export function isDotNetCoreProject(project: MSBuildProject): boolean {
return findNetCoreTargetFramework(project) !== undefined ||
findNetStandardTargetFramework(project) !== undefined ||
findNetFrameworkTargetFramework(project) !== undefined;
Expand Down
5 changes: 3 additions & 2 deletions src/omnisharp/requestQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class RequestQueue {
this.eventStream.post(new OmnisharpServerProcessRequestStart(this._name, availableRequestSlots));

for (let i = 0; i < availableRequestSlots && this._pending.length > 0; i++) {
const item = this._pending.shift();
const item = this._pending.shift()!; // We know from this._pending.length that we can still shift
item.startTime = Date.now();

const id = this._makeRequest(item);
Expand All @@ -117,6 +117,7 @@ export class RequestQueueCollection {
concurrency: number,
makeRequest: (request: Request) => number
) {
this._isProcessing = false;
this._priorityQueue = new RequestQueue('Priority', 1, eventStream, makeRequest);
this._normalQueue = new RequestQueue('Normal', concurrency, eventStream, makeRequest);
this._deferredQueue = new RequestQueue('Deferred', Math.max(Math.floor(concurrency / 4), 2), eventStream, makeRequest);
Expand Down Expand Up @@ -188,4 +189,4 @@ export class RequestQueueCollection {

this._isProcessing = false;
}
}
}
Loading