Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror cursors #213

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@
"description": "Enable/disable autoclosing of XML tags. \n\nIMPORTANT: Turn off editor.autoClosingTags for this to work",
"scope": "window"
},
"xml.mirrorCursorOnMatchingTag": {
"type": "boolean",
"scope": "resource",
"default": false,
"description": "Adds an additional cursor on the matching tag, allows for start/end tag editing."
},
"xml.codeLens.enabled": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -295,6 +301,14 @@
"fileMatch": "package.json",
"url": "./schemas/package.schema.json"
}
],
"keybindings":[
{
"command": "xml.toggleMatchingTagEdit",
"key": "ctrl+shift+f2",
"mac": "cmd+shift+f2",
"when": "editorFocus"
}
]
}
}
52 changes: 50 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
import { prepareExecutable } from './javaServerStarter';
import { LanguageClientOptions, RevealOutputChannelOn, LanguageClient, DidChangeConfigurationNotification, RequestType, TextDocumentPositionParams, ReferencesRequest } from 'vscode-languageclient';
import * as requirements from './requirements';
import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, LanguageConfiguration, Uri, extensions } from "vscode";
import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, LanguageConfiguration, Uri, extensions, WorkspaceConfiguration, ConfigurationTarget } from "vscode";
import * as path from 'path';
import * as os from 'os';
import { activateTagClosing, AutoCloseResult } from './tagClosing';
import { Commands } from './commands';
import { onConfigurationChange, subscribeJDKChangeConfiguration } from './settings';
import { collectXmlJavaExtensions, onExtensionChange } from './plugin';
import { setupMirrorCursor } from './mirrorCursor';

export interface ScopeInfo {
scope : "default" | "global" | "workspace" | "folder";
Expand All @@ -30,7 +31,9 @@ namespace TagCloseRequest {
export const type: RequestType<TextDocumentPositionParams, AutoCloseResult, any, any> = new RequestType('xml/closeTag');
}


namespace MatchingTagPositionRequest {
export const type: RequestType<TextDocumentPositionParams, Position | null, any, any> = new RequestType('xml/matchingTagPosition');
}

export function activate(context: ExtensionContext) {
let storagePath = context.storagePath;
Expand Down Expand Up @@ -122,6 +125,30 @@ export function activate(context: ExtensionContext) {

disposable = activateTagClosing(tagProvider, { xml: true, xsl: true }, Commands.AUTO_CLOSE_TAGS);
toDispose.push(disposable);

//Setup mirrored tag rename request
const matchingTagPositionRequestor = (document: TextDocument, position: Position) => {
let param = languageClient.code2ProtocolConverter.asTextDocumentPositionParams(document, position);
return languageClient.sendRequest(MatchingTagPositionRequest.type, param);
};

toDispose.push(setupMirrorCursor(matchingTagPositionRequestor, ['xml', 'xsl']));

const matchingTagEditCommand = 'xml.toggleMatchingTagEdit';

const matchingTagEditHandler = async () => {
let xmlConfiguration: WorkspaceConfiguration;
if (window.activeTextEditor) {
xmlConfiguration = workspace.getConfiguration('xml', window.activeTextEditor.document.uri);
} else {
xmlConfiguration = workspace.getConfiguration('xml');
}
const current = xmlConfiguration.mirrorCursorOnMatchingTag;
await updateConfig(xmlConfiguration, 'mirrorCursorOnMatchingTag', !current);
}

toDispose.push(commands.registerCommand(matchingTagEditCommand, matchingTagEditHandler));

});
languages.setLanguageConfiguration('xml', getIndentationRules());
languages.setLanguageConfiguration('xsl', getIndentationRules());
Expand Down Expand Up @@ -192,3 +219,24 @@ function getIndentationRules(): LanguageConfiguration {
};
}

/**
* Update config with the following precedence: WorkspaceFolder -> Workspace -> Global
* @param config config containing the section to update
* @param section section to update
* @param value new value
*/
async function updateConfig(config: WorkspaceConfiguration, section: string, value: any): Promise<void> {
try {
await config.update(section, value);
return;
} catch(e) {
// try ConfigurationTarget.Global
}

try {
await config.update(section, value, ConfigurationTarget.Global);
return;
} catch(e) {
throw 'Failed to update config';
}
}
Loading