Skip to content

Commit

Permalink
Make standalone connect start working again
Browse files Browse the repository at this point in the history
- Creates a temporary project root if no workspace folder present

Fixes #814
  • Loading branch information
PEZ committed Feb 14, 2021
1 parent 28b8fa0 commit 005c54b
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 101 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"Insourcing",
"Jackin",
"Jackout",
"Libspec",
"Notespace",
"OVSX",
"Pseudoterminal",
"REBL",
Expand Down Expand Up @@ -66,6 +68,7 @@
"entrypoint",
"errored",
"eval",
"evals",
"falsesomething",
"fehse",
"feldman",
Expand Down Expand Up @@ -120,6 +123,7 @@
"sbin",
"scaturro",
"schäfer",
"seqs",
"sivertsen",
"stacktraces",
"stian",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Changes to Calva.

## [Unreleased]
- Fix [Connect ”not in project” glitches](https://github.com/BetterThanTomorrow/calva/issues/814)

## [2.0.171] - 2021-02-10
- Update clojure-lsp to version 2021.02.09-18.28.06 (Fix: [Auto completion does not work in clojure-lsp only mode (no repl connection)](https://github.com/BetterThanTomorrow/calva/issues/996#issuecomment-776148282))
Expand Down
22 changes: 6 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"onLanguage:clojure",
"onCommand:calva.startNewRepl",
"onCommand:calva.jackIn",
"onCommand:calva.jackInOrConnect",
"onCommand:calva.startOrConnectRepl",
"onCommand:calva.connect",
"onCommand:calva.connectNonProjectREPL",
"workspaceContains:**/project.clj",
Expand Down Expand Up @@ -658,14 +658,8 @@
"category": "Calva"
},
{
"command": "calva.jackInOrConnect",
"title": "Jack-in or Connect to REPL Server",
"enablement": "!calva:connected && !calva:connecting",
"category": "Calva"
},
{
"command": "calva.startNewRepl",
"title": "Start a Clojure REPL",
"command": "calva.startOrConnectRepl",
"title": "Start or Connect to a Clojure REPL",
"category": "Calva"
},
{
Expand Down Expand Up @@ -1241,7 +1235,7 @@
"when": "calva:keybindingsEnabled"
},
{
"command": "calva.startNewRepl",
"command": "calva.startOrConnectRepl",
"key": "ctrl+alt+c ctrl+alt+r"
},
{
Expand Down Expand Up @@ -1724,11 +1718,7 @@
"menus": {
"commandPalette": [
{
"command": "calva.jackInOrConnect",
"when": "false"
},
{
"command": "calva.startNewRepl",
"command": "calva.startOrConnectRepl",
"when": "true"
},
{
Expand Down Expand Up @@ -1767,7 +1757,7 @@
{
"when": "editorLangId == clojure",
"enablement": "!calva:connected",
"command": "calva.jackInOrConnect",
"command": "calva.startOrConnectRepl",
"group": "calva/x-connect"
},
{
Expand Down
27 changes: 19 additions & 8 deletions src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,13 @@ export async function connect(connectSequence: ReplConnectSequence,

try {
if (port === undefined) {
let bytes = await vscode.workspace.fs.readFile(portFile);
port = new TextDecoder("utf-8").decode(bytes);
try {
await vscode.workspace.fs.stat(portFile);
let bytes = await vscode.workspace.fs.readFile(portFile);
port = new TextDecoder("utf-8").decode(bytes);
} catch {
throw new Error("No nrepl port found");
}
}
if (port) {
hostname = hostname !== undefined ? hostname : "localhost";
Expand All @@ -476,7 +481,13 @@ export async function connect(connectSequence: ReplConnectSequence,

}

async function standaloneConnect(connectSequence: ReplConnectSequence) {
async function standaloneConnect(context: vscode.ExtensionContext, connectSequence: ReplConnectSequence) {
await state.initProjectDir();
let projectDirUri = state.getProjectRootUri();
if (!projectDirUri) {
projectDirUri = await state.getOrCreateNonProjectRoot(context);
}
await state.initProjectDir(projectDirUri);
await outputWindow.initResultsDoc();
await outputWindow.openResultsDoc();

Expand All @@ -492,25 +503,25 @@ async function standaloneConnect(connectSequence: ReplConnectSequence) {
}

export default {
connectNonProjectREPLCommand: async () => {
connectNonProjectREPLCommand: async (context: vscode.ExtensionContext) => {
status.updateNeedReplUi(true);
const connectSequence = await askForConnectSequence(projectTypes.getAllProjectTypes(), 'connect-type', "ConnectInterrupted");
standaloneConnect(connectSequence);
standaloneConnect(context, connectSequence);
},
connectCommand: async () => {
connectCommand: async (context: vscode.ExtensionContext) => {
status.updateNeedReplUi(true);
// TODO: Figure out a better way to have an initialized project directory.
try {
await state.initProjectDir();
await liveShareSupport.setupLiveShareListener();
} catch {
// Could be a bae file, user makes the call
vscode.commands.executeCommand('calva.jackInOrConnect');
vscode.commands.executeCommand('calva.startOrConnectRepl');
return;
}
const cljTypes = await projectTypes.detectProjectTypes(),
connectSequence = await askForConnectSequence(cljTypes, 'connect-type', "ConnectInterrupted");
standaloneConnect(connectSequence);
standaloneConnect(context, connectSequence);
},
disconnect: (options = null, callback = () => { }) => {
status.updateNeedReplUi(false);
Expand Down
8 changes: 5 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ async function activate(context: vscode.ExtensionContext) {
status.update(context);

// COMMANDS
context.subscriptions.push(vscode.commands.registerCommand('calva.jackInOrConnect', jackIn.calvaJackInOrConnect));
context.subscriptions.push(vscode.commands.registerCommand('calva.startNewRepl', jackIn.calvaJackInOrConnect));
context.subscriptions.push(vscode.commands.registerCommand('calva.startOrConnectRepl', jackIn.startOrConnectRepl));
context.subscriptions.push(vscode.commands.registerCommand('calva.startNewRepl', jackIn.startOrConnectRepl));
context.subscriptions.push(vscode.commands.registerCommand('calva.jackIn', jackIn.calvaJackIn));
context.subscriptions.push(vscode.commands.registerCommand('calva.copyJackInCommandToClipboard', jackIn.copyJackInCommandToClipboard));
context.subscriptions.push(vscode.commands.registerCommand('calva.connectNonProjectREPL', connector.connectNonProjectREPLCommand));
context.subscriptions.push(vscode.commands.registerCommand('calva.connectNonProjectREPL', () => {
connector.connectNonProjectREPLCommand(context)
}));
context.subscriptions.push(vscode.commands.registerCommand('calva.connect', connector.connectCommand));
context.subscriptions.push(vscode.commands.registerCommand('calva.disconnect', jackIn.calvaDisconnect));
context.subscriptions.push(vscode.commands.registerCommand('calva.toggleCLJCSession', connector.toggleCLJCSession));
Expand Down
2 changes: 1 addition & 1 deletion src/nrepl/connectSequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function getCustomConnectSequences(): ReplConnectSequence[] {
*/
function getConnectSequences(projectTypes: string[]): ReplConnectSequence[] {
const customSequences = getCustomConnectSequences();
const defSequences = projectTypes.reduce((seqs, projecType) => seqs.concat(defaultSequences[projecType]), []);
const defSequences = projectTypes.reduce((seqs, projectType) => seqs.concat(defaultSequences[projectType]), []);
const defSequenceProjectTypes = [...new Set(defSequences.map(s => s.projectType))];
const sequences = customSequences.filter(customSequence => defSequenceProjectTypes.includes(customSequence.projectType)).concat(defSequences);
return sequences;
Expand Down
2 changes: 1 addition & 1 deletion src/nrepl/jack-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export async function calvaDisconnect() {
vscode.window.showInformationMessage("Not connected to a REPL server");
}

export async function calvaJackInOrConnect() {
export async function startOrConnectRepl() {
const JACK_IN_OPTION = "Start a REPL server and connect (a.k.a. Jack-in)";
const JACK_IN_COMMAND = "calva.jackIn";
let commands = {};
Expand Down
5 changes: 3 additions & 2 deletions src/nrepl/project-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as pprint from '../printer';

import { keywordize, unKeywordize } from '../util/string';
import { CljsTypes, ReplConnectSequence } from './connectSequence';
import { pathToNs } from '../util/ns-form';
const { parseForms, parseEdn } = require('../../out/cljs-lib/cljs-lib');

export const isWin = /^win/.test(process.platform);
Expand Down Expand Up @@ -60,7 +61,7 @@ export function nreplPortFileUri(connectSequence: ReplConnectSequence): vscode.U
try {
return vscode.Uri.joinPath(projectRoot, relativePath);
} catch (e) {
console.log(e);
return vscode.Uri.file(path.join(projectRoot.fsPath, relativePath));
}
}
return vscode.Uri.file(relativePath);
Expand Down Expand Up @@ -495,7 +496,7 @@ export async function detectProjectTypes(): Promise<string[]> {
}

export function getAllProjectTypes(): string[] {
return [...Object.keys(projectTypes)];
return ['generic', ...Object.keys(projectTypes).filter(pt => pt !== 'generic')];
}

export function getCljsTypeName(connectSequence: ReplConnectSequence) {
Expand Down
8 changes: 5 additions & 3 deletions src/results-output/results-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ export const CLJS_CONNECT_GREETINGS = '; TIPS: You can choose which REPL to use
; *Calva: Toggle REPL connection*\n\
; (There is a button in the status bar for this)';


function outputFileDir() {
let projectRoot = state.getProjectRootUri();
if (!projectRoot) {
projectRoot = vscode.Uri.file(os.tmpdir());
try {
return vscode.Uri.joinPath(projectRoot, ".calva", "output-window");
} catch {
return vscode.Uri.file(path.join(projectRoot.fsPath, ".calva", "output-window"));
}
return vscode.Uri.joinPath(projectRoot, ".calva", "output-window");
};

const DOC_URI = () => {
Expand Down
Loading

0 comments on commit 005c54b

Please sign in to comment.