Skip to content

Commit

Permalink
Apply on_enter edits from the client (#158)
Browse files Browse the repository at this point in the history
* Apply on_enter edits from the client

* Default to false
  • Loading branch information
sdankel authored Jul 31, 2023
1 parent 18b0159 commit 836a4b7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
29 changes: 0 additions & 29 deletions client/src/interface/getRunnables.ts

This file was deleted.

49 changes: 49 additions & 0 deletions client/src/interface/onEnter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Range, TextDocumentChangeEvent, window } from 'vscode';
import {
RequestType,
TextDocumentContentChangeEvent,
TextDocumentEdit,
TextDocumentIdentifier,
WorkspaceEdit,
} from 'vscode-languageclient/node';
import { getClient } from '../client';
import { ProgramType } from '../program';
import { toVSCodeRange } from '../util/convert';
import { addFilePrefix } from '../util/util';

interface OnEnterParams {
textDocument: TextDocumentIdentifier;
contentChanges: TextDocumentContentChangeEvent[];
}

export type Runnable = [Range, ProgramType];

const request = new RequestType<OnEnterParams, WorkspaceEdit | null, void>(
'sway/on_enter'
);

export const onEnter = async (changeEvent: TextDocumentChangeEvent) => {
if (
changeEvent.document.uri.scheme === 'file' &&
changeEvent.contentChanges.length === 1 &&
changeEvent.contentChanges[0].text.includes('\n')
) {
const client = getClient();
const params: OnEnterParams = {
textDocument: {
uri: addFilePrefix(changeEvent.document.uri.fsPath),
},
contentChanges: [...changeEvent.contentChanges],
};
const response = await client.sendRequest(request, params);
if (!!response) {
window.activeTextEditor.edit(editBuilder => {
response.documentChanges?.forEach((change: TextDocumentEdit) => {
change.edits.forEach(edit => {
editBuilder.replace(toVSCodeRange(edit.range), edit.newText);
});
});
});
}
}
};
6 changes: 6 additions & 0 deletions client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { commands, ExtensionContext, window, workspace } from 'vscode';
import * as lc from 'vscode-languageclient/node';
import { createClient, getClient } from './client';
import { Config } from './config';
import { onEnter } from './interface/onEnter';
import { CommandPalettes } from './palettes';
import updateFuelCoreStatus from './status_bar/fuelCoreStatus';
import { log } from './util/util';
Expand All @@ -24,6 +25,11 @@ export async function activate(context: ExtensionContext) {
// Start a recurring task to keep fuel-core status updated
setInterval(updateFuelCoreStatus, 1000);

// Listen for did_change events for on_enter capabilities.
workspace.onDidChangeTextDocument(
async changeEvent => await onEnter(changeEvent)
);

try {
const client = createClient(getClientOptions(), await getServerOptions());

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
},
"sway-lsp.onEnter.continueDocComments": {
"markdownDescription": "Whether to continue /// comments when enter is pressed.",
"default": true,
"default": false,
"type": "boolean"
},
"sway-lsp.onEnter.continueComments": {
Expand Down

0 comments on commit 836a4b7

Please sign in to comment.