Skip to content

Commit e054452

Browse files
committed
(GH-412) Replace ConnectionManager with ConnectionHandler
This commit replaces the use of `ConnectionManager` with the indiviual `ConnectionHandler` implementations for STDIO and TCP inside `extension.ts`. It also replaces `ConnectionManager` with `ConnectionHandler` inside all of the ExtensionFeatures, which is a drop-in replacement due to the signature being the same.
1 parent b8b4bc2 commit e054452

File tree

4 files changed

+78
-50
lines changed

4 files changed

+78
-50
lines changed

src/extension.ts

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,76 @@
33
import * as vscode from 'vscode';
44
import * as fs from 'fs';
55

6-
import { ConnectionManager } from './connection';
76
import { ConnectionConfiguration } from './configuration';
8-
import { OutputChannelLogger } from './logging/outputchannel';
9-
import { Reporter } from './telemetry/telemetry';
7+
import { ConnectionHandler } from './handler';
8+
import { StdioConnectionHandler } from './handlers/stdio';
9+
import { TcpConnectionHandler } from './handlers/tcp';
1010
import { IFeature } from './feature';
11-
import { PuppetStatusBar } from './PuppetStatusBar';
12-
import { ISettings, legacySettings, settingsFromWorkspace } from './settings';
1311
import { DebugConfigurationFeature } from './feature/DebugConfigurationFeature';
1412
import { FormatDocumentFeature } from './feature/FormatDocumentFeature';
1513
import { NodeGraphFeature } from './feature/NodeGraphFeature';
1614
import { PDKFeature } from './feature/PDKFeature';
1715
import { PuppetResourceFeature } from './feature/PuppetResourceFeature';
16+
import { ProtocolType, ConnectionType, IConnectionConfiguration } from './interfaces';
1817
import { ILogger } from './logging';
19-
import { ProtocolType, ConnectionType } from './interfaces';
18+
import { OutputChannelLogger } from './logging/outputchannel';
19+
import { PuppetCommandStrings } from './messages';
20+
import { PuppetStatusBar } from './PuppetStatusBar';
21+
import { ISettings, legacySettings, settingsFromWorkspace } from './settings';
22+
import { Reporter } from './telemetry/telemetry';
2023

21-
var connManager: ConnectionManager;
2224
const langID = 'puppet'; // don't change this
25+
let extContext: vscode.ExtensionContext;
26+
let connectionHandler: ConnectionHandler;
27+
let settings: ISettings;
28+
let logger: OutputChannelLogger;
29+
let statusBar: PuppetStatusBar;
30+
let configSettings: IConnectionConfiguration;
2331
let extensionFeatures: IFeature[] = [];
2432

2533
export function activate(context: vscode.ExtensionContext) {
26-
notifyOnNewExtensionVersion(context);
34+
extContext = context;
35+
36+
notifyOnNewExtensionVersion(extContext);
2737
checkForLegacySettings();
2838

29-
context.subscriptions.push(new Reporter(context));
30-
const settings: ISettings = settingsFromWorkspace();
31-
var logger = new OutputChannelLogger(settings),
32-
statusBar = new PuppetStatusBar(langID, context, logger),
33-
configSettings = new ConnectionConfiguration();
39+
context.subscriptions.push(new Reporter(extContext));
3440

35-
connManager = new ConnectionManager(context, logger, statusBar, configSettings);
41+
settings = settingsFromWorkspace();
42+
logger = new OutputChannelLogger(settings);
43+
statusBar = new PuppetStatusBar(langID, context, logger);
44+
configSettings = new ConnectionConfiguration();
3645

37-
checkInstallDirectory(configSettings, logger);
46+
if(checkInstallDirectory(configSettings, logger) === false){
47+
// If this returns false, then we needed a local directory
48+
// but did not find it, so we should abort here
49+
// If we return true, we can continue
50+
// This can be revisited to enable disabling language server portion
51+
return;
52+
}
53+
54+
switch (configSettings.protocol) {
55+
case ProtocolType.STDIO:
56+
connectionHandler = new StdioConnectionHandler(extContext, settings, statusBar, logger, configSettings);
57+
break;
58+
case ProtocolType.TCP:
59+
connectionHandler = new TcpConnectionHandler(extContext, settings, statusBar, logger, configSettings);
60+
break;
61+
}
3862

3963
extensionFeatures = [
40-
new DebugConfigurationFeature(logger, context),
41-
new FormatDocumentFeature(langID, connManager, settings, logger, context),
42-
new NodeGraphFeature(langID, connManager, logger, context),
43-
new PDKFeature(context, logger),
44-
new PuppetResourceFeature(context, connManager, logger)
64+
new DebugConfigurationFeature(logger, extContext),
65+
new FormatDocumentFeature(langID, connectionHandler, settings, logger, extContext),
66+
new NodeGraphFeature(langID, connectionHandler, logger, extContext),
67+
new PDKFeature(extContext, logger),
68+
new PuppetResourceFeature(extContext, connectionHandler, logger)
4569
];
4670

47-
connManager.start(configSettings);
71+
extContext.subscriptions.push(vscode.commands.registerCommand(PuppetCommandStrings.PuppetRestartSessionCommandId,
72+
() => {
73+
74+
}
75+
));
4876
}
4977

5078
export function deactivate() {
@@ -53,9 +81,8 @@ export function deactivate() {
5381
feature.dispose();
5482
});
5583

56-
if (connManager !== undefined) {
57-
connManager.stop();
58-
connManager.dispose();
84+
if (connectionHandler !== undefined) {
85+
connectionHandler.stop();
5986
}
6087
}
6188

@@ -76,11 +103,11 @@ function checkForLegacySettings() {
76103
}
77104
}
78105

79-
function checkInstallDirectory(configSettings: ConnectionConfiguration, logger: ILogger) {
106+
function checkInstallDirectory(configSettings: IConnectionConfiguration, logger: ILogger) : boolean {
80107
if(configSettings.protocol === ProtocolType.TCP){
81108
if(configSettings.type === ConnectionType.Remote){
82109
// Return if we are connecting to a remote TCP LangServer
83-
return;
110+
return true;
84111
}
85112
}
86113

@@ -94,9 +121,10 @@ function checkInstallDirectory(configSettings: ConnectionConfiguration, logger:
94121
'https://github.com/lingua-pupuli/puppet-vscode#experience-a-problem',
95122
logger
96123
);
97-
return null;
124+
return false;
98125
} else {
99126
logger.debug('Found a valid Puppet installation at ' + configSettings.puppetDir);
127+
return true;
100128
}
101129
}
102130

src/feature/FormatDocumentFeature.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
import * as vscode from "vscode";
44
import { IFeature } from "../feature";
55
import { ILogger } from "../logging";
6-
import { IConnectionManager } from '../connection';
76
import { ConnectionStatus } from '../interfaces';
87
import { ISettings } from '../settings';
98
import * as messages from '../messages';
9+
import { ConnectionHandler } from "../handler";
1010

1111
class RequestParams implements messages.PuppetFixDiagnosticErrorsRequestParams {
1212
documentUri: string;
1313
alwaysReturnContent: boolean;
1414
}
1515

1616
class FormatDocumentProvider {
17-
private connectionManager: IConnectionManager = undefined;
17+
private connectionHandler: ConnectionHandler = undefined;
1818

19-
constructor(connectionManager: IConnectionManager) {
20-
this.connectionManager = connectionManager;
19+
constructor(connectionManager: ConnectionHandler) {
20+
this.connectionHandler = connectionManager;
2121
}
2222

2323
public async formatTextEdits(document: vscode.TextDocument, options: vscode.FormattingOptions): Promise<vscode.TextEdit[]> {
24-
if ((this.connectionManager.status !== ConnectionStatus.RunningLoaded) && (this.connectionManager.status !== ConnectionStatus.RunningLoading)) {
24+
if ((this.connectionHandler.status !== ConnectionStatus.RunningLoaded) && (this.connectionHandler.status !== ConnectionStatus.RunningLoading)) {
2525
vscode.window.showInformationMessage("Please wait and try again. The Puppet extension is still loading...");
2626
return [];
2727
}
@@ -30,7 +30,7 @@ class FormatDocumentProvider {
3030
requestParams.documentUri = document.uri.toString(false);
3131
requestParams.alwaysReturnContent = false;
3232

33-
const result = await this.connectionManager
33+
const result = await this.connectionHandler
3434
.languageClient
3535
.sendRequest(messages.PuppetFixDiagnosticErrorsRequest.type, requestParams) as messages.PuppetFixDiagnosticErrorsResponse;
3636
if (result.fixesApplied > 0 && result.newContent !== undefined) {
@@ -45,7 +45,7 @@ export class FormatDocumentFeature implements IFeature {
4545

4646
constructor(
4747
langID: string,
48-
connectionManager: IConnectionManager,
48+
connectionManager: ConnectionHandler,
4949
settings: ISettings,
5050
logger: ILogger,
5151
context: vscode.ExtensionContext

src/feature/NodeGraphFeature.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ import * as path from 'path';
55

66
import { IFeature } from "../feature";
77
import { ILogger } from "../logging";
8-
import { IConnectionManager } from '../connection';
98
import { ConnectionStatus } from '../interfaces';
109
import { CompileNodeGraphRequest } from '../messages';
1110
import { reporter } from '../telemetry/telemetry';
1211
import * as viz from 'viz.js';
12+
import { ConnectionHandler } from "../handler";
1313

1414
const PuppetNodeGraphToTheSideCommandId: string = 'extension.puppetShowNodeGraphToSide';
1515

1616
class NodeGraphWebViewProvider implements vscode.Disposable {
17-
private connectionManager: IConnectionManager = undefined;
17+
private connectionHandler: ConnectionHandler = undefined;
1818
private docUri: vscode.Uri = undefined;
1919
private webPanel: vscode.WebviewPanel = undefined;
2020
private parentFeature: NodeGraphFeature = undefined;
2121
private shownLanguageServerNotAvailable = false;
2222

2323
constructor(
2424
documentUri:vscode.Uri,
25-
connectionManager:IConnectionManager,
25+
connectionManager:ConnectionHandler,
2626
parent: NodeGraphFeature)
2727
{
2828
this.docUri = documentUri;
29-
this.connectionManager = connectionManager;
29+
this.connectionHandler = connectionManager;
3030
this.parentFeature = parent;
3131
}
3232

@@ -55,7 +55,7 @@ class NodeGraphWebViewProvider implements vscode.Disposable {
5555
}
5656

5757
public async getHTMLContent(): Promise<string> {
58-
if ((this.connectionManager.status !== ConnectionStatus.RunningLoaded) && (this.connectionManager.status !== ConnectionStatus.RunningLoading)) {
58+
if ((this.connectionHandler.status !== ConnectionStatus.RunningLoaded) && (this.connectionHandler.status !== ConnectionStatus.RunningLoading)) {
5959
if (this.shownLanguageServerNotAvailable) {
6060
vscode.window.showInformationMessage("The Puppet Node Graph Preview is not available as the Editor Service is not ready");
6161
this.shownLanguageServerNotAvailable = true;
@@ -67,7 +67,7 @@ class NodeGraphWebViewProvider implements vscode.Disposable {
6767
const requestData = {
6868
external: this.docUri.toString()
6969
};
70-
return this.connectionManager.languageClient
70+
return this.connectionHandler.languageClient
7171
.sendRequest(CompileNodeGraphRequest.type, requestData)
7272
.then(
7373
(compileResult) => {
@@ -143,7 +143,7 @@ ${svgContent}
143143
export class NodeGraphFeature implements IFeature {
144144
private acceptedLangId: string = undefined;
145145
private providers: NodeGraphWebViewProvider[] = undefined;
146-
private connectionManager: IConnectionManager = undefined;
146+
private connectionHandler: ConnectionHandler = undefined;
147147

148148
public onProviderWebPanelDisposed(provider: NodeGraphWebViewProvider): void {
149149
// If the panel gets disposed then the user closed the tab.
@@ -157,21 +157,21 @@ export class NodeGraphFeature implements IFeature {
157157

158158
constructor(
159159
langID: string,
160-
connectionManager: IConnectionManager,
160+
connectionHandler: ConnectionHandler,
161161
logger: ILogger,
162162
context: vscode.ExtensionContext
163163
) {
164164
this.acceptedLangId = langID;
165165
this.providers = [];
166-
this.connectionManager = connectionManager;
166+
this.connectionHandler = connectionHandler;
167167

168168
context.subscriptions.push(vscode.commands.registerCommand(PuppetNodeGraphToTheSideCommandId,
169169
() => {
170170
if (!vscode.window.activeTextEditor) { return; }
171171
if (vscode.window.activeTextEditor.document.languageId !== this.acceptedLangId) { return; }
172172

173173
let resource = vscode.window.activeTextEditor.document.uri;
174-
let provider = new NodeGraphWebViewProvider(resource, this.connectionManager, this);
174+
let provider = new NodeGraphWebViewProvider(resource, this.connectionHandler, this);
175175
this.providers.push(provider);
176176
provider.show();
177177
}

src/feature/PuppetResourceFeature.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import { IFeature } from '../feature';
55
import { ILogger } from '../logging';
66
import { PuppetCommandStrings, PuppetResourceRequestParams, PuppetResourceRequest } from '../messages';
77
import { reporter } from '../telemetry/telemetry';
8-
import { IConnectionManager } from '../connection';
98
import { ConnectionStatus } from '../interfaces';
9+
import { ConnectionHandler } from '../handler';
1010

1111
export class PuppetResourceFeature implements IFeature {
12-
private _connectionManager: IConnectionManager;
12+
private _connectionHandler: ConnectionHandler;
1313
private logger: ILogger;
1414

1515
dispose() {}
1616

17-
constructor(context: vscode.ExtensionContext, connMgr: IConnectionManager, logger: ILogger) {
17+
constructor(context: vscode.ExtensionContext, connMgr: ConnectionHandler, logger: ILogger) {
1818
this.logger = logger;
19-
this._connectionManager = connMgr;
19+
this._connectionHandler = connMgr;
2020
context.subscriptions.push(
2121
vscode.commands.registerCommand(PuppetCommandStrings.PuppetResourceCommandId, () => {
2222
this.run();
@@ -26,7 +26,7 @@ export class PuppetResourceFeature implements IFeature {
2626
public run() {
2727
var thisCommand = this;
2828

29-
if (thisCommand._connectionManager.status !== ConnectionStatus.RunningLoaded) {
29+
if (thisCommand._connectionHandler.status !== ConnectionStatus.RunningLoaded) {
3030
vscode.window.showInformationMessage('Puppet Resource is not available as the Language Server is not ready');
3131
return;
3232
}
@@ -42,7 +42,7 @@ export class PuppetResourceFeature implements IFeature {
4242
let requestParams = new RequestParams();
4343
requestParams.typename = moduleName;
4444

45-
thisCommand._connectionManager.languageClient
45+
thisCommand._connectionHandler.languageClient
4646
.sendRequest(PuppetResourceRequest.type, requestParams)
4747
.then(resourceResult => {
4848
if (resourceResult.error !== undefined && resourceResult.error.length > 0) {

0 commit comments

Comments
 (0)