Skip to content

Commit 91fe4a6

Browse files
authored
Merge pull request #31 from jpogran/extensionts-cleanup
Extension.ts cleanup
2 parents 0df1fb9 + 8b3468b commit 91fe4a6

File tree

5 files changed

+132
-113
lines changed

5 files changed

+132
-113
lines changed

client/src/extension.ts

Lines changed: 7 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
'use strict';
22

33
import * as vscode from 'vscode';
4-
import * as path from 'path';
5-
import * as net from 'net';
6-
import * as cp from 'child_process';
7-
import ChildProcess = cp.ChildProcess;
8-
import {
9-
LanguageClient, LanguageClientOptions, SettingMonitor, ServerOptions,
10-
ErrorAction, ErrorHandler, CloseAction, TransportKind, RequestType0
11-
} from 'vscode-languageclient';
124

13-
import { PuppetNodeGraphContentProvider, isNodeGraphFile, getNodeGraphUri } from '../src/providers/previewNodeGraphProvider';
14-
import { puppetResourceCommand } from '../src/commands/puppetResourceCommand';
15-
import { puppetModuleCommand } from '../src/commands/puppetModuleCommand';
16-
import * as messages from '../src/messages';
5+
import { startLangServerTCP } from '../src/languageserver';
6+
import { setupPuppetCommands } from '../src/puppetcommands';
177

18-
const langID = 'puppet';
8+
const langID = 'puppet'; // don't change this
199
var statusBarItem;
20-
var languageServerClient: LanguageClient = undefined;
2110

2211
export function activate(context: vscode.ExtensionContext) {
2312
let config = vscode.workspace.getConfiguration('puppet');
@@ -30,34 +19,10 @@ export function activate(context: vscode.ExtensionContext) {
3019

3120
createStatusBarItem();
3221

33-
context.subscriptions.push(startLangServerTCP(host, port, langID, [langID]));
22+
var languageServerClient = startLangServerTCP(host, port, langID, statusBarItem);
23+
context.subscriptions.push(languageServerClient.start());
3424

35-
let resourceCommand = new puppetResourceCommand(languageServerClient);
36-
context.subscriptions.push(resourceCommand);
37-
context.subscriptions.push(vscode.commands.registerCommand('extension.puppetResource', () => {
38-
resourceCommand.run();
39-
}));
40-
41-
let moduleCommand = new puppetModuleCommand();
42-
context.subscriptions.push(moduleCommand);
43-
context.subscriptions.push(vscode.commands.registerCommand('extension.puppetModule', () => {
44-
moduleCommand.listModules();
45-
}));
46-
47-
context.subscriptions.push(vscode.commands.registerCommand(
48-
'extension.puppetShowNodeGraphToSide',
49-
uri => showNodeGraph(uri, true))
50-
);
51-
52-
const contentProvider = new PuppetNodeGraphContentProvider(context, languageServerClient);
53-
const contentProviderRegistration = vscode.workspace.registerTextDocumentContentProvider(langID, contentProvider);
54-
55-
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(document => {
56-
if (isNodeGraphFile(document)) {
57-
const uri = getNodeGraphUri(document.uri);
58-
contentProvider.update(uri);
59-
}
60-
}));
25+
setupPuppetCommands(langID, languageServerClient, context);
6126

6227
console.log('Congratulations, your extension "vscode-puppet" is now active!');
6328
}
@@ -66,45 +31,11 @@ export function activate(context: vscode.ExtensionContext) {
6631
export function deactivate() {
6732
}
6833

69-
function startLangServerTCP(host: string, port: number, langID: string, documentSelector: string | string[]): vscode.Disposable {
70-
let serverOptions: ServerOptions = function () {
71-
return new Promise((resolve, reject) => {
72-
var client = new net.Socket();
73-
client.connect(port, host, function () {
74-
resolve({ reader: client, writer: client });
75-
});
76-
client.on('error', function (err) {
77-
console.log(`[Puppet Lang Server Client] ` + err);
78-
})
79-
});
80-
}
81-
82-
let clientOptions: LanguageClientOptions = {
83-
documentSelector: [langID],
84-
}
85-
86-
var title = `tcp lang server (host ${host} port ${port})`;
87-
languageServerClient = new LanguageClient(title, serverOptions, clientOptions)
88-
languageServerClient.onReady().then(() => {
89-
languageServerClient.sendRequest(messages.PuppetVersionRequest.type).then((versionDetails) => {
90-
statusBarItem.color = "#affc74";
91-
statusBarItem.text = "$(terminal) " + versionDetails.puppetVersion;
92-
});
93-
}, (reason) => {
94-
this.setSessionFailure("Could not start language service: ", reason);
95-
});
96-
97-
return languageServerClient.start();
98-
}
99-
10034
// Status Bar handler
10135
export function createStatusBarItem() {
10236
if (statusBarItem === undefined) {
103-
// Create the status bar item and place it right next
104-
// to the language indicator
37+
// Create the status bar item and place it right next to the language indicator
10538
statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 1);
106-
107-
//this.statusBarItem.command = this.ShowSessionMenuCommandName;
10839
statusBarItem.show();
10940
vscode.window.onDidChangeActiveTextEditor(textEditor => {
11041
if (textEditor === undefined || textEditor.document.languageId !== "puppet") {
@@ -117,40 +48,3 @@ export function createStatusBarItem() {
11748
}
11849
}
11950

120-
function showNodeGraph(uri?: vscode.Uri, sideBySide: boolean = false) {
121-
let resource = uri;
122-
if (!(resource instanceof vscode.Uri)) {
123-
if (vscode.window.activeTextEditor) {
124-
// we are relaxed and don't check for puppet files
125-
// TODO: Should we? Probably
126-
resource = vscode.window.activeTextEditor.document.uri;
127-
}
128-
}
129-
130-
const thenable = vscode.commands.executeCommand('vscode.previewHtml',
131-
getNodeGraphUri(resource),
132-
getViewColumn(sideBySide),
133-
`Node Graph '${path.basename(resource.fsPath)}'`);
134-
135-
return thenable;
136-
}
137-
138-
function getViewColumn(sideBySide: boolean): vscode.ViewColumn | undefined {
139-
const active = vscode.window.activeTextEditor;
140-
if (!active) {
141-
return vscode.ViewColumn.One;
142-
}
143-
144-
if (!sideBySide) {
145-
return active.viewColumn;
146-
}
147-
148-
switch (active.viewColumn) {
149-
case vscode.ViewColumn.One:
150-
return vscode.ViewColumn.Two;
151-
case vscode.ViewColumn.Two:
152-
return vscode.ViewColumn.Three;
153-
}
154-
155-
return active.viewColumn;
156-
}

client/src/languageserver.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {
2+
LanguageClient, LanguageClientOptions, ServerOptions
3+
} from 'vscode-languageclient'
4+
import * as vscode from 'vscode';
5+
import * as net from 'net';
6+
import * as cp from 'child_process';
7+
import ChildProcess = cp.ChildProcess;
8+
9+
import * as messages from '../src/messages';
10+
11+
export function startLangServerTCP(host: string, port: number, langID: string, statusBarItem): LanguageClient {
12+
let serverOptions: ServerOptions = function () {
13+
return new Promise((resolve, reject) => {
14+
var client = new net.Socket();
15+
client.connect(port, host, function () {
16+
resolve({ reader: client, writer: client });
17+
});
18+
client.on('error', function (err) {
19+
console.log(`[Puppet Lang Server Client] ` + err);
20+
})
21+
});
22+
}
23+
24+
let clientOptions: LanguageClientOptions = {
25+
documentSelector: [langID],
26+
}
27+
28+
var title = `tcp lang server (host ${host} port ${port})`;
29+
var languageServerClient = new LanguageClient(title, serverOptions, clientOptions)
30+
languageServerClient.onReady().then(() => {
31+
languageServerClient.sendRequest(messages.PuppetVersionRequest.type).then((versionDetails) => {
32+
statusBarItem.color = "#affc74";
33+
statusBarItem.text = "$(terminal) " + versionDetails.puppetVersion;
34+
});
35+
}, (reason) => {
36+
this.setSessionFailure("Could not start language service: ", reason);
37+
});
38+
39+
return languageServerClient;
40+
}

client/src/messages.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ export interface CompileNodeGraphResponse {
3232
error: string;
3333
data: string;
3434
}
35+
36+
export class PuppetCommandStrings{
37+
static PuppetResourceCommandId:string = 'extension.puppetResource';
38+
static PuppetModuleCommandId:string = 'extension.puppetModule';
39+
static PuppetNodeGraphToTheSideCommandId = 'extension.puppetShowNodeGraphToSide';
40+
}

client/src/providers/previewNodeGraphProvider.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,41 @@ export class PuppetNodeGraphContentProvider implements vscode.TextDocumentConten
113113
}
114114
}
115115
}
116+
117+
export function showNodeGraph(uri?: vscode.Uri, sideBySide: boolean = false) {
118+
let resource = uri;
119+
if (!(resource instanceof vscode.Uri)) {
120+
if (vscode.window.activeTextEditor) {
121+
// we are relaxed and don't check for puppet files
122+
// TODO: Should we? Probably
123+
resource = vscode.window.activeTextEditor.document.uri;
124+
}
125+
}
126+
127+
const thenable = vscode.commands.executeCommand('vscode.previewHtml',
128+
getNodeGraphUri(resource),
129+
getViewColumn(sideBySide),
130+
`Node Graph '${path.basename(resource.fsPath)}'`);
131+
132+
return thenable;
133+
}
134+
135+
export function getViewColumn(sideBySide: boolean): vscode.ViewColumn | undefined {
136+
const active = vscode.window.activeTextEditor;
137+
if (!active) {
138+
return vscode.ViewColumn.One;
139+
}
140+
141+
if (!sideBySide) {
142+
return active.viewColumn;
143+
}
144+
145+
switch (active.viewColumn) {
146+
case vscode.ViewColumn.One:
147+
return vscode.ViewColumn.Two;
148+
case vscode.ViewColumn.Two:
149+
return vscode.ViewColumn.Three;
150+
}
151+
152+
return active.viewColumn;
153+
}

client/src/puppetcommands.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as vscode from 'vscode';
2+
import {
3+
LanguageClient, LanguageClientOptions, ServerOptions
4+
} from 'vscode-languageclient'
5+
6+
import { puppetResourceCommand } from '../src/commands/puppetResourceCommand';
7+
import { puppetModuleCommand } from '../src/commands/puppetModuleCommand';
8+
import * as messages from '../src/messages';
9+
import {
10+
PuppetNodeGraphContentProvider, isNodeGraphFile, getNodeGraphUri,
11+
showNodeGraph, getViewColumn
12+
} from '../src/providers/previewNodeGraphProvider';
13+
14+
export function setupPuppetCommands(langID:string, languageServerClient:LanguageClient, ctx:vscode.ExtensionContext){
15+
16+
let resourceCommand = new puppetResourceCommand(languageServerClient);
17+
ctx.subscriptions.push(resourceCommand);
18+
ctx.subscriptions.push(vscode.commands.registerCommand(messages.PuppetCommandStrings.PuppetResourceCommandId, () => {
19+
resourceCommand.run();
20+
}));
21+
22+
let moduleCommand = new puppetModuleCommand();
23+
ctx.subscriptions.push(moduleCommand);
24+
ctx.subscriptions.push(vscode.commands.registerCommand(messages.PuppetCommandStrings.PuppetModuleCommandId, () => {
25+
moduleCommand.listModules();
26+
}));
27+
28+
ctx.subscriptions.push(vscode.commands.registerCommand(messages.PuppetCommandStrings.PuppetNodeGraphToTheSideCommandId,
29+
uri => showNodeGraph(uri, true))
30+
);
31+
32+
const contentProvider = new PuppetNodeGraphContentProvider(ctx, languageServerClient);
33+
const contentProviderRegistration = vscode.workspace.registerTextDocumentContentProvider(langID, contentProvider);
34+
35+
ctx.subscriptions.push(vscode.workspace.onDidSaveTextDocument(document => {
36+
if (isNodeGraphFile(document)) {
37+
const uri = getNodeGraphUri(document.uri);
38+
contentProvider.update(uri);
39+
}
40+
}));
41+
}

0 commit comments

Comments
 (0)