Skip to content

Commit

Permalink
show type info with completion on TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
sixmen committed May 4, 2020
1 parent 34d68ca commit f82094d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
31 changes: 20 additions & 11 deletions lib/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function createTsEval(accumulatedCode) {
}
};
}
function replaceCompleter(replServer) {
function replaceCompleter(replServer, accumulatedCode) {
const originalCompleter = replServer.completer;
replServer.completer = (line, callback) => {
const hasExtraChars = /(?:\(|\s)/.test(line);
Expand All @@ -104,16 +104,25 @@ function replaceCompleter(replServer) {
callback(error, result);
return;
}
replServer.eval(result[1], replServer.context, 'repl_complete', (e, object) => {
if (typeof (object) === 'function') {
const argsMatch = object.toString().match(/^function\s*[^(]*\(\s*([^)]*)\)/m)
|| object.toString().match(/^[^(]*\(\s*([^)]*)\)/m);
replServer.output.write(os_1.default.EOL);
replServer.output.write(`${result[1]}(\u001b[35m${argsMatch[1]}\u001b[39m)\r\n`);
replServer._refreshLine();
}
const input = `${accumulatedCode.input}${result[1]}`;
const typeInfo = register.getTypeInfo(input, '[eval].ts', input.length);
if (/^(?:function\s*|const [^:]*:\s*|let [^:]*:\s*|var [^:]*:\s*)[^(]*\(\s*([^)]*)\)/m.exec(typeInfo.name)) {
replServer.output.write(os_1.default.EOL);
replServer.output.write(`${result[1]}(\u001b[35m${RegExp.$1}\u001b[39m)\r\n`);
callback(error, [[result[1]], result[1]]);
});
}
else {
replServer.eval(result[1], replServer.context, 'repl_complete', (e, object) => {
if (typeof (object) === 'function') {
const argsMatch = object.toString().match(/^function\s*[^(]*\(\s*([^)]*)\)/m)
|| object.toString().match(/^[^(]*\(\s*([^)]*)\)/m);
replServer.output.write(os_1.default.EOL);
replServer.output.write(`${result[1]}(\u001b[35m${argsMatch[1]}\u001b[39m)\r\n`);
replServer._refreshLine();
}
callback(error, [[result[1]], result[1]]);
});
}
});
};
}
Expand Down Expand Up @@ -187,7 +196,7 @@ exports.start = (rinoreOptions) => {
});
history_1.setupHistory(replServer, rinoreOptions.historyFile || '.rinore_history_ts', 1000);
context_1.setupContext(replServer);
replaceCompleter(replServer);
replaceCompleter(replServer, accumulatedCode);
setupAccumulatedCodeInput(accumulatedCode);
vm_1.default.runInContext('exports = module.exports', replServer.context);
return replServer;
Expand Down
30 changes: 19 additions & 11 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function createTsEval(accumulatedCode: { input: string; output: string }) {
};
}

function replaceCompleter(replServer: any) {
function replaceCompleter(replServer: any, accumulatedCode: { input: string; output: string }) {
const originalCompleter = replServer.completer;
replServer.completer = (line: string, callback: (error?: any, result?: any) => void) => {
const hasExtraChars = /(?:\(|\s)/.test(line);
Expand All @@ -104,16 +104,24 @@ function replaceCompleter(replServer: any) {
callback(error, result);
return;
}
replServer.eval(result[1], replServer.context, 'repl_complete', (e?: any, object?: any) => {
if (typeof (object) === 'function') {
const argsMatch = object.toString().match(/^function\s*[^(]*\(\s*([^)]*)\)/m)
|| object.toString().match(/^[^(]*\(\s*([^)]*)\)/m);
replServer.output.write(os.EOL);
replServer.output.write(`${result[1]}(\u001b[35m${argsMatch[1]}\u001b[39m)\r\n`);
replServer._refreshLine();
}
const input = `${accumulatedCode.input}${result[1]}`;
const typeInfo = register.getTypeInfo(input, '[eval].ts', input.length);
if (/^(?:function\s*|const [^:]*:\s*|let [^:]*:\s*|var [^:]*:\s*)[^(]*\(\s*([^)]*)\)/m.exec(typeInfo.name)) {
replServer.output.write(os.EOL);
replServer.output.write(`${result[1]}(\u001b[35m${RegExp.$1}\u001b[39m)\r\n`);
callback(error, [[result[1]], result[1]]);
});
} else {
replServer.eval(result[1], replServer.context, 'repl_complete', (e?: any, object?: any) => {
if (typeof (object) === 'function') {
const argsMatch = object.toString().match(/^function\s*[^(]*\(\s*([^)]*)\)/m)
|| object.toString().match(/^[^(]*\(\s*([^)]*)\)/m);
replServer.output.write(os.EOL);
replServer.output.write(`${result[1]}(\u001b[35m${argsMatch[1]}\u001b[39m)\r\n`);
replServer._refreshLine();
}
callback(error, [[result[1]], result[1]]);
});
}
});
};
}
Expand Down Expand Up @@ -187,7 +195,7 @@ export const start = (rinoreOptions: RinoreOptions): repl.REPLServer => {
});
setupHistory(replServer, rinoreOptions.historyFile || '.rinore_history_ts', 1000);
setupContext(replServer);
replaceCompleter(replServer);
replaceCompleter(replServer, accumulatedCode);
setupAccumulatedCodeInput(accumulatedCode);
vm.runInContext('exports = module.exports', replServer.context);
return replServer;
Expand Down
4 changes: 2 additions & 2 deletions test/typescript/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('complete', () => {
const runList: string[] = [];
const code = 'fs.readFile(';
const expectedResult: [string[], string] = [['fs.readFile'], 'fs.readFile'];
const expectedOutput: string[] = ['fs.readFile(\u001b[35mpath, options, callback\u001b[39m)'];
const expectedOutput: string[] = ['fs.readFile(\u001b[35mpath: string | number | Buffer | url.URL, options: {\n encoding?: null | undefined;\n flag?: string | undefined;\n} | null | undefined, callback: (err: NodeJS.ErrnoException | null, data: Buffer\u001b[39m)'];
if (process.version.startsWith('v6.')) {
expectedOutput[0] = expectedOutput[0].replace('callback', 'callback_');
}
Expand All @@ -174,7 +174,7 @@ describe('complete', () => {
const runList: string[] = [];
const code = 'fs.readFile ';
const expectedResult: [string[], string] = [['fs.readFile'], 'fs.readFile'];
const expectedOutput: string[] = ['fs.readFile(\u001b[35mpath, options, callback\u001b[39m)'];
const expectedOutput: string[] = ['fs.readFile(\u001b[35mpath: string | number | Buffer | url.URL, options: {\n encoding?: null | undefined;\n flag?: string | undefined;\n} | null | undefined, callback: (err: NodeJS.ErrnoException | null, data: Buffer\u001b[39m)'];
if (process.version.startsWith('v6.')) {
expectedOutput[0] = expectedOutput[0].replace('callback', 'callback_');
}
Expand Down

0 comments on commit f82094d

Please sign in to comment.