Skip to content

Commit

Permalink
refactor code (#124)
Browse files Browse the repository at this point in the history
- classes derive from IDisposable
- use class member initialzers
  • Loading branch information
jmue authored Sep 6, 2022
1 parent c298feb commit 21a5eaf
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 136 deletions.
14 changes: 7 additions & 7 deletions src/ccAnnotateController.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {
DecorationOptions,
DecorationRenderOptions,
ExtensionContext,
Range,
TextEditor,
TextEditorDecorationType,
window,
} from "vscode";
import { CCConfigHandler } from "./ccConfigHandler";
import { CCConfiguration } from "./ccConfiguration";
import { IDisposable } from "./model";

export class CCAnnotationController {
export class CCAnnotationController implements IDisposable {
private mDecorationType: TextEditorDecorationType;
private mIsActive: boolean;
private mIsActive = false;
private mConfiguration: CCConfiguration;
private mDisposables: IDisposable[] = [];

constructor(private editor: TextEditor, private context: ExtensionContext, private configHandler: CCConfigHandler) {
this.mIsActive = false;
window.onDidChangeActiveTextEditor((editor) => this.onActiveEditorChange(editor), this, this.context.subscriptions);
this.configHandler.onDidChangeConfiguration(() => this.onConfigurationChanged());
constructor(private editor: TextEditor, private configHandler: CCConfigHandler) {
this.mDisposables.push(window.onDidChangeActiveTextEditor((editor) => this.onActiveEditorChange(editor)));
this.mDisposables.push(this.configHandler.onDidChangeConfiguration(() => this.onConfigurationChanged()));
const ro: DecorationRenderOptions = {
isWholeLine: false,
before: {
Expand Down
36 changes: 22 additions & 14 deletions src/ccAnnotateLensProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodeLens, CodeLensProvider, ExtensionContext, Range, TextDocument } from "vscode";
import { CancellationToken, CodeLens, CodeLensProvider, ProviderResult, Range, TextDocument } from "vscode";
import { CCAnnotateLens } from "./ccAnnotateLens";
import { CCConfigHandler } from "./ccConfigHandler";
import { CCScmProvider } from "./ccScmProvider";
Expand All @@ -9,35 +9,43 @@ export class CCCodeLensProvider implements CodeLensProvider {
};

constructor(
private mContext: ExtensionContext,
private mCfg: CCConfigHandler,
private mProvider: CCScmProvider
) { }

provideCodeLenses(document: TextDocument): Thenable<CodeLens[]> | CodeLens[] {
provideCodeLenses(document: TextDocument, token: CancellationToken): ProviderResult<CodeLens[]> {
if (!this.mCfg.configuration.showAnnotationCodeLens.value) {
return [];
}

return new Promise((resolve) => {
this.mProvider.clearCase?.isClearcaseObject(document.uri).then((is: boolean) => {
const lLenses: CodeLens[] = [];
if (document !== undefined && is === true) {
lLenses.push(new CCAnnotateLens(document, new Range(0, 0, 0, 1)));
}
resolve(lLenses);
});
});
return this.getCodeLenses(document, token);
}

resolveCodeLens(codeLens: CodeLens): Thenable<CodeLens> {
private async getCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
if (token.isCancellationRequested === true) {
return [];
}

const isClearcaseObject = (await this.mProvider.clearCase?.isClearcaseObject(document.uri)) ?? false;

if (document !== undefined && isClearcaseObject) {
return [new CCAnnotateLens(document, new Range(0, 0, 0, 1))];
}
return [];
}

resolveCodeLens?(codeLens: CodeLens, token: CancellationToken): ProviderResult<CodeLens> {
if (token.isCancellationRequested === true) {
return codeLens;
}

if (codeLens instanceof CCAnnotateLens) {
codeLens.command = {
title: "Toggle annotations",
command: "extension.ccAnnotate",
arguments: [codeLens.document.uri],
};
return Promise.resolve(codeLens);
return codeLens;
}

return Promise.reject();
Expand Down
28 changes: 14 additions & 14 deletions src/ccConfigHandler.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
"use strict";

import { Disposable, Event, EventEmitter, ExtensionContext, workspace, WorkspaceConfiguration } from "vscode";
import { Event, EventEmitter, workspace, WorkspaceConfiguration } from "vscode";
import { CCConfiguration, ConfigurationProperty } from "./ccConfiguration";
import { IDisposable } from "./model";

export class CCConfigHandler {
private mConfigChanged: EventEmitter<string[]>;
private mConfiguration: CCConfiguration;
private mChangeIdents: string[];

constructor(private context: ExtensionContext, private disposables: Disposable[]) {
this.mChangeIdents = [];
this.mConfigChanged = new EventEmitter<string[]>();
this.mConfiguration = new CCConfiguration();
export class CCConfigHandler implements IDisposable {
private mConfigChanged = new EventEmitter<string[]>();
private mConfiguration = new CCConfiguration();
private mChangeIdents: string[] = [];
private mDisposables: IDisposable[] = [];

constructor() {
this.loadConfig();

this.disposables.push(
workspace.onDidChangeConfiguration(() => this.handleChangedConfig(), this, this.context.subscriptions)
);
this.mDisposables.push(workspace.onDidChangeConfiguration(() => this.handleChangedConfig()));
}

dispose(): void {
this.mDisposables.forEach((d) => d.dispose());
}

get onDidChangeConfiguration(): Event<string[]> {
Expand Down Expand Up @@ -76,7 +76,7 @@ export class CCConfigHandler {
}
return false;
}

private handleChangedConfig(): void {
if (this.loadConfig()) {
this.mConfigChanged.fire(this.mChangeIdents);
Expand Down
34 changes: 28 additions & 6 deletions src/ccContentProvider.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { workspace, Uri, Disposable, TextDocumentContentProvider, QuickDiffProvider, CancellationToken } from "vscode";
import {
workspace,
Uri,
TextDocumentContentProvider,
QuickDiffProvider,
CancellationToken,
ProviderResult,
} from "vscode";
import { ClearCase } from "./clearcase";
import { IDisposable } from "./model";
import { toCcUri, fromCcUri } from "./uri";

export class CCContentProvider implements TextDocumentContentProvider, QuickDiffProvider, Disposable {
constructor(private mCcHandler: ClearCase | null, private mDisposals: Disposable[]) {
export class CCContentProvider implements TextDocumentContentProvider, QuickDiffProvider, IDisposable {
private mDisposals: IDisposable[] = [];

constructor(private mCcHandler: ClearCase | null) {
if (this.mCcHandler !== null) {
this.mDisposals.push(workspace.registerTextDocumentContentProvider("cc", this));
this.mDisposals.push(workspace.registerTextDocumentContentProvider("cc-orig", this));
}
}

async provideTextDocumentContent(uri: Uri, token: CancellationToken): Promise<string> {
provideTextDocumentContent(uri: Uri, token: CancellationToken): ProviderResult<string> {
return this.getTextDocumentContent(uri, token);
}

private async getTextDocumentContent(uri: Uri, token: CancellationToken): Promise<string> {
if (token.isCancellationRequested === true) {
return "canceled";
}
Expand All @@ -30,12 +44,20 @@ export class CCContentProvider implements TextDocumentContentProvider, QuickDiff
return "";
}

async provideOriginalResource(uri: Uri): Promise<Uri | undefined> {
provideOriginalResource(uri: Uri, token: CancellationToken): ProviderResult<Uri> {
return this.getOriginalResource(uri, token);
}

async getOriginalResource(uri: Uri, token?: CancellationToken): Promise<Uri | undefined> {
if (token?.isCancellationRequested === true) {
return undefined;
}

if (uri.scheme !== "file") {
return;
}

const currentVersion = this.mCcHandler ? await this.mCcHandler.getVersionInformation(uri, false) : "";
const currentVersion = (await this.mCcHandler?.getVersionInformation(uri, false)) ?? "";
if (currentVersion !== "") {
const isCheckedOut = currentVersion.match("\\b(CHECKEDOUT)\\b$");

Expand Down
21 changes: 7 additions & 14 deletions src/ccIgnoreHandler.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
import { workspace, WorkspaceFolder, Uri, EventEmitter } from "vscode";
import { workspace, WorkspaceFolder, Uri, EventEmitter, Event } from "vscode";
import { existsSync, readFileSync, statSync } from "fs";
import { join, dirname, sep } from "path";
import ignore, { Ignore } from "ignore";
import { ModelHandler } from "./model";

export class IgnoreHandler {
private fileIgnores: FileIgnore[];
private mOnFilterRefreshed: EventEmitter<void>;
private fileIgnores: FileIgnore[] = [];
private mOnFilterRefreshed = new EventEmitter<void>();

constructor(private mFsWatch: ModelHandler) {
this.mOnFilterRefreshed = new EventEmitter<void>();
this.fileIgnores = [];
this.init();
}

get onFilterRefreshed(): EventEmitter<void> {
return this.mOnFilterRefreshed;
}

private init(): void {
this.fileIgnores = [];
workspace.workspaceFolders?.forEach((folder: WorkspaceFolder) => {
const lM = this.mFsWatch.addWatcher(join(folder.uri.fsPath, ".ccignore"));
lM.onWorkspaceChanged((fileObj) => this.refreshFilter(fileObj));
Expand All @@ -30,6 +19,10 @@ export class IgnoreHandler {
});
}

get onFilterRefreshed(): Event<void> {
return this.mOnFilterRefreshed.event;
}

getFolderIgnore(path: Uri | string): FileIgnore | null {
const t = this.appendSeparator(typeof path === "string" ? path : path.fsPath);
for (const ignore of this.fileIgnores) {
Expand Down
42 changes: 17 additions & 25 deletions src/ccScmProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
scm,
SourceControlResourceGroup,
Uri,
Disposable,
OutputChannel,
commands,
workspace,
Expand All @@ -25,7 +24,7 @@ import { CCScmResource, ResourceGroupType } from "./ccScmResource";
import { CCScmStatus } from "./ccScmStatus";
import { ClearCase, ViewType } from "./clearcase";
import { LocalizeFunc, loadMessageBundle } from "vscode-nls";
import { ModelHandler } from "./model";
import { IDisposable, ModelHandler } from "./model";
import { CCConfigHandler } from "./ccConfigHandler";
import { CCAnnotationController } from "./ccAnnotateController";
import { CCCodeLensProvider } from "./ccAnnotateLensProvider";
Expand All @@ -40,7 +39,7 @@ import { getErrorMessage } from "./errormessage";

const localize: LocalizeFunc = loadMessageBundle();

export class CCScmProvider {
export class CCScmProvider implements IDisposable {
private mCCContentProvider: CCContentProvider | null = null;
private mCCHandler: ClearCase | null = null;
private mIgnoreFileEv: ModelHandler | null = null;
Expand All @@ -50,6 +49,7 @@ export class CCScmProvider {
private mIsUpdatingUntracked: boolean | null = null;
private mListLock: Lock | null = null;
private mIgnores: IgnoreHandler | null = null;
private mDisposables: IDisposable[] = [];

private mWindowChangedEvent: EventEmitter<void> = new EventEmitter<void>();

Expand All @@ -62,14 +62,13 @@ export class CCScmProvider {

constructor(
private mContext: ExtensionContext,
private mDisposables: Disposable[],
private outputChannel: OutputChannel,
private configHandler: CCConfigHandler
) { }

async init(): Promise<boolean> {
this.mListLock = new Lock(1);
this.mCCHandler = new ClearCase(this.mContext, this.configHandler, this.outputChannel);
this.mCCHandler = new ClearCase(this.configHandler, this.outputChannel);
if (this.configHandler.configuration.useRemoteClient.value === true) {
if (this.configHandler.configuration.webserverPassword.value !== "") {
if (this.clearCase) {
Expand Down Expand Up @@ -110,9 +109,9 @@ export class CCScmProvider {
}

private async startExtension(): Promise<boolean> {
let isView: boolean | undefined = false;
let isView = false;
try {
isView = await this.mCCHandler?.checkIsView(undefined);
isView = (await this.mCCHandler?.checkIsView(undefined)) ?? false;
} catch (error) {
isView = false;
}
Expand All @@ -129,9 +128,10 @@ export class CCScmProvider {
this.mCCUntrackedGrp = this.mCCScm.createResourceGroup("cc_untracked", "View private");
this.mCCCheckedoutGrp.hideWhenEmpty = true;
this.mCCUntrackedGrp.hideWhenEmpty = true;
this.mCCContentProvider = new CCContentProvider(this.mCCHandler, this.mDisposables);
this.mCCContentProvider = new CCContentProvider(this.mCCHandler);

this.mContext.subscriptions.push(this.mCCScm);
this.mDisposables.push(this.mCCScm);
this.mDisposables.push(this.mCCContentProvider);

this.mCCScm.inputBox.placeholder = "Message (press Ctrl+Enter to checkin all files)";
this.mCCScm.acceptInputCommand = {
Expand All @@ -143,11 +143,8 @@ export class CCScmProvider {
}

this.mIgnoreFileEv = new ModelHandler();
this.mIgnoreFileEv.init();
this.mIgnores = new IgnoreHandler(this.mIgnoreFileEv);
this.mIgnores.onFilterRefreshed.event(() => {
this.filterUntrackedList();
}, this);
this.mIgnores.onFilterRefreshed(() => this.filterUntrackedList());

this.clearCase?.onCommandExecuted((evArgs: Uri) => {
this.handleChangeFiles(evArgs);
Expand All @@ -169,13 +166,8 @@ export class CCScmProvider {
return this.mCCHandler;
}

updateIsView(): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
this.clearCase
?.checkIsView(window.activeTextEditor)
.then(() => resolve(this.clearCase?.isView ?? false))
.catch(() => reject(false));
});
async updateIsView(): Promise<boolean> {
return this.clearCase?.checkIsView(window.activeTextEditor) ?? false;
}

private async handleChangeFiles(fileObj: Uri) {
Expand Down Expand Up @@ -502,9 +494,9 @@ export class CCScmProvider {
);

if (window.activeTextEditor !== undefined) {
const annoCtrl = new CCAnnotationController(window.activeTextEditor, this.mContext, this.configHandler);
const annoCtrl = new CCAnnotationController(window.activeTextEditor, this.configHandler);
this.mDisposables.push(annoCtrl);

this.mContext.subscriptions.push(annoCtrl);
this.mDisposables.push(
commands.registerCommand(
"extension.ccAnnotate",
Expand All @@ -518,10 +510,10 @@ export class CCScmProvider {
);
}

this.mContext.subscriptions.push(
this.mDisposables.push(
languages.registerCodeLensProvider(
CCCodeLensProvider.selector,
new CCCodeLensProvider(this.mContext, this.configHandler, this)
new CCCodeLensProvider(this.configHandler, this)
)
);
}
Expand Down Expand Up @@ -689,7 +681,7 @@ export class CCScmProvider {
preview: true,
};

const prevUri = await this.mCCContentProvider?.provideOriginalResource(fileObj);
const prevUri = await this.mCCContentProvider?.getOriginalResource(fileObj);
if (prevUri !== undefined) {
const fn = path.basename(fileObj.fsPath);
const { version } = fromCcUri(prevUri);
Expand Down
Loading

0 comments on commit 21a5eaf

Please sign in to comment.