Skip to content

Commit cad6e18

Browse files
committed
tooltips (the last sync thing I know of) are now async \o/ refs #27
Also starts work on ref #52. Will be removing "messages.ts" as its almost redundant in the new mechanism.
1 parent 547f9bd commit cad6e18

9 files changed

+84
-31
lines changed

lib/globals.ts

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010

1111
/// <reference path="./node_modules/views/views.d.ts"/>
1212
/// <reference path="./typings/atompromise.d.ts"/>
13+
14+
interface Function{
15+
name?: string; // exists for named function on node / atom / "good" browsers ;)
16+
}

lib/main/atom/tooltipManager.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
var programManager = require('../lang/programManager');
21
var atomUtils = require('./atomUtils');
2+
var parent = require('../../worker/parent');
33
var path = require('path');
44
var fs = require('fs');
5-
var ts = require('typescript');
65
var emissary = require('emissary');
76
var Subscriber = emissary.Subscriber;
87
var TooltipView = require('views/tooltip');
@@ -16,7 +15,6 @@ function attach(editorView) {
1615
if (!fs.existsSync(filePath)) {
1716
return;
1817
}
19-
var program = programManager.getOrCreateProgram(filePath);
2018
var scroll = editorView.find('.scroll-view');
2119
var subscriber = new Subscriber();
2220
var exprTypeTimeout = null;
@@ -46,18 +44,18 @@ function attach(editorView) {
4644
};
4745
exprTypeTooltip = new TooltipView(tooltipRect);
4846
var position = atomUtils.getEditorPositionForBufferPosition(editor, bufferPt);
49-
var info = program.languageService.getQuickInfoAtPosition(filePath, position);
50-
if (!info) {
51-
hideExpressionType();
52-
}
53-
else {
54-
var displayName = ts.displayPartsToString(info.displayParts || []);
55-
var documentation = ts.displayPartsToString(info.documentation || []);
56-
var message = "<b>" + displayName + "</b>";
57-
if (documentation)
58-
message = message + ("<br/><i>" + documentation + "</i>");
59-
exprTypeTooltip.updateText(message);
60-
}
47+
parent.quickInfo({ filePath: filePath, position: position }).then(function (resp) {
48+
if (!resp.valid) {
49+
hideExpressionType();
50+
}
51+
else {
52+
var message = "<b>" + resp.name + "</b>";
53+
if (resp.comment)
54+
message = message + ("<br/><i>" + resp.comment + "</i>");
55+
if (exprTypeTooltip)
56+
exprTypeTooltip.updateText(message);
57+
}
58+
});
6159
}
6260
function deactivate() {
6361
subscriber.unsubscribe();

lib/main/atom/tooltipManager.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
///ts:ref=globals
22
/// <reference path="../../globals.ts"/> ///ts:ref:generated
33

4-
///ts:import=programManager
5-
import programManager = require('../lang/programManager'); ///ts:import:generated
64
///ts:import=atomUtils
75
import atomUtils = require('./atomUtils'); ///ts:import:generated
6+
///ts:import=parent
7+
import parent = require('../../worker/parent'); ///ts:import:generated
88

99
import path = require('path');
1010
import fs = require('fs');
@@ -26,8 +26,6 @@ export function attach(editorView: any) {
2626
return;
2727
}
2828

29-
var program = programManager.getOrCreateProgram(filePath);
30-
3129
var scroll = editorView.find('.scroll-view');
3230
var subscriber = new Subscriber();
3331
var exprTypeTimeout = null;
@@ -65,18 +63,19 @@ export function attach(editorView: any) {
6563
};
6664
exprTypeTooltip = new TooltipView(tooltipRect);
6765

66+
var position = atomUtils.getEditorPositionForBufferPosition(editor, bufferPt);
6867
// Actually make the program manager query
69-
var position = atomUtils.getEditorPositionForBufferPosition(editor,bufferPt);
70-
var info = program.languageService.getQuickInfoAtPosition(filePath, position);
71-
if (!info) {
72-
hideExpressionType();
73-
} else {
74-
var displayName = ts.displayPartsToString(info.displayParts || []);
75-
var documentation = ts.displayPartsToString(info.documentation || []);
76-
var message = `<b>${displayName}</b>`;
77-
if(documentation) message = message + `<br/><i>${documentation}</i>`;
78-
exprTypeTooltip.updateText(message);
79-
}
68+
parent.quickInfo({ filePath, position }).then((resp) => {
69+
if (!resp.valid) {
70+
hideExpressionType();
71+
}
72+
else {
73+
var message = `<b>${resp.name}</b>`;
74+
if (resp.comment) message = message + `<br/><i>${resp.comment}</i>`;
75+
// Sorry about this "if". It's in the code I copied so I guess its there for a reason
76+
if (exprTypeTooltip) exprTypeTooltip.updateText(message);
77+
}
78+
});
8079
}
8180

8281

lib/main/lang/programManager.js

+13
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,16 @@ function getCompletionsAtPosition(filePath, position, prefix) {
206206
});
207207
}
208208
exports.getCompletionsAtPosition = getCompletionsAtPosition;
209+
function quickInfo(query) {
210+
var program = getOrCreateProgram(query.filePath);
211+
var info = program.languageService.getQuickInfoAtPosition(query.filePath, query.position);
212+
if (!info)
213+
return { valid: false };
214+
else
215+
return {
216+
valid: true,
217+
name: ts.displayPartsToString(info.displayParts || []),
218+
comment: ts.displayPartsToString(info.documentation || []),
219+
};
220+
}
221+
exports.quickInfo = quickInfo;

lib/main/lang/programManager.ts

+20
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,23 @@ export function getCompletionsAtPosition(filePath: string, position: number, pre
281281
};
282282
});
283283
}
284+
285+
export interface QuickInfoResponse {
286+
valid: boolean; // Do we have a valid response for this query
287+
name?: string;
288+
comment?: string;
289+
}
290+
export interface QuickInfoQuery {
291+
filePath: string;
292+
position: number;
293+
}
294+
export function quickInfo(query: QuickInfoQuery): QuickInfoResponse {
295+
var program = getOrCreateProgram(query.filePath);
296+
var info = program.languageService.getQuickInfoAtPosition(query.filePath, query.position);
297+
if (!info) return { valid: false };
298+
else return {
299+
valid: true,
300+
name: ts.displayPartsToString(info.displayParts || []),
301+
comment: ts.displayPartsToString(info.documentation || []),
302+
}
303+
}

lib/worker/parent.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var messages = require('./messages');
2+
var programManager = require('../main/lang/programManager');
23
var childprocess = require('child_process');
34
var os = require('os');
45
var exec = childprocess.exec;
@@ -99,3 +100,4 @@ exports.getErrorsForFile = function (data) { return query(messages.getErrorsForF
99100
exports.getCompletionsAtPosition = function (data) { return query(messages.getCompletionsAtPosition, data); };
100101
exports.getErrorsForFileFiltered = function (data) { return query(messages.getErrorsForFileFiltered, data); };
101102
exports.build = function (data) { return query(messages.build, data); };
103+
exports.quickInfo = function (data) { return query(programManager.quickInfo.name, data); };

lib/worker/parent.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
///ts:import=messages
55
import messages = require('./messages'); ///ts:import:generated
6+
///ts:import=programManager
7+
import programManager = require('../main/lang/programManager'); ///ts:import:generated
68

79
import childprocess = require('child_process');
810
import os = require('os');
@@ -127,7 +129,9 @@ function showError(error?: Error) {
127129
}
128130

129131
/////////////////////////////////////// END INFRASTRUCTURE ////////////////////////////////////////////////////
130-
132+
// Infrastructure note
133+
// This could be simplified if there was a way in TypeScript to capture the type information for first argument / return values
134+
// But there isn't as far as I know. TODO: I do have other cunning ideas though
131135

132136
export var echo: Exec<messages.EchoQuery, messages.EchoResponse>
133137
= (data) => query(messages.echo, data);
@@ -146,3 +150,6 @@ export var getErrorsForFileFiltered: Exec<messages.GetErrorsForFileFilteredQuery
146150

147151
export var build: Exec<messages.BuildQuery, messages.BuildResponse>
148152
= (data) => query(messages.build, data);
153+
154+
export var quickInfo: Exec<programManager.QuickInfoQuery,programManager.QuickInfoResponse>
155+
= (data) => query(programManager.quickInfo.name, data);

lib/worker/workerProcess.js

+4
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ responders[messages.build] = function (data) {
5151
outputs: programManager.getOrCreateProgram(data.filePath).build()
5252
};
5353
};
54+
function addToResponders(func) {
55+
responders[func.name] = func;
56+
}
57+
addToResponders(programManager.quickInfo);

lib/worker/workerProcess.ts

+6
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,9 @@ responders[messages.build] = (data: messages.BuildQuery): messages.BuildResponse
7272
outputs: programManager.getOrCreateProgram(data.filePath).build()
7373
};
7474
}
75+
76+
function addToResponders<Query, Response>(func: (query: Query) => Response) {
77+
responders[func.name] = func;
78+
}
79+
80+
addToResponders(programManager.quickInfo);

0 commit comments

Comments
 (0)