-
Notifications
You must be signed in to change notification settings - Fork 97
Allows editing of start/end tag simultaneously #204
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
Closed
NikolasKomonen
wants to merge
1
commit into
redhat-developer:master
from
NikolasKomonen:matchingTagClient
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { | ||
| window, | ||
| workspace, | ||
| Disposable, | ||
| TextDocument, | ||
| Position, | ||
| TextEditorSelectionChangeEvent, | ||
| Selection, | ||
| Range, | ||
| WorkspaceEdit | ||
| } from 'vscode'; | ||
|
|
||
| export function activateMirrorCursor( | ||
| matchingTagPositionProvider: (document: TextDocument, position: Position) => Thenable<Position | null>, | ||
| supportedLanguages: { [id: string]: boolean }, | ||
| configName: string | ||
| ): Disposable { | ||
| let disposables: Disposable[] = []; | ||
|
|
||
| window.onDidChangeTextEditorSelection(event => onDidChangeTextEditorSelection(event), null, disposables); | ||
| let previousState = workspace.getConfiguration().get<boolean>(configName); | ||
| let wasNotified = false; | ||
| let isEnabled = false; | ||
| updateEnabledState(); | ||
|
|
||
| workspace.onDidChangeConfiguration(updateEnabledState, null, disposables); | ||
|
|
||
| function updateEnabledState() { | ||
| updateStateSetting(); | ||
| promptUpdateMessage(); | ||
| } | ||
|
|
||
| function updateStateSetting() { | ||
| isEnabled = false; | ||
| let editor = window.activeTextEditor; | ||
| if (!editor) { | ||
| return; | ||
| } | ||
| let document = editor.document; | ||
| if (!supportedLanguages[document.languageId]) { | ||
| return; | ||
| } | ||
| if (!workspace.getConfiguration(undefined, document.uri).get<boolean>(configName)) { | ||
| return; | ||
| } | ||
| isEnabled = true; | ||
| } | ||
|
|
||
| function promptUpdateMessage() { | ||
| if(!wasNotified && previousState != isEnabled) { | ||
| window.showInformationMessage("Toggled the `xml.matchingTagEditing` preference in the Workspace settings.") | ||
| wasNotified = true; | ||
| } | ||
| previousState = isEnabled; | ||
| } | ||
|
|
||
| let prevCursors: readonly Selection[] = []; | ||
| let cursors: readonly Selection[] = []; | ||
| let inMirrorMode = false; | ||
|
|
||
| function onDidChangeTextEditorSelection(event: TextEditorSelectionChangeEvent) { | ||
| if (!isEnabled) { | ||
| return; | ||
| } | ||
|
|
||
| prevCursors = cursors; | ||
| cursors = event.selections; | ||
|
|
||
| if (cursors.length === 1) { | ||
| if (inMirrorMode && prevCursors.length === 2) { | ||
| if (cursors[0].isEqual(prevCursors[0]) || cursors[0].isEqual(prevCursors[1])) { | ||
| return; | ||
| } | ||
| } | ||
| if (event.selections[0].isEmpty) { | ||
| matchingTagPositionProvider(event.textEditor.document, event.selections[0].active).then(matchingTagPosition => { | ||
| if (matchingTagPosition && window.activeTextEditor) { | ||
| const charBeforeAndAfterPositionsRoughtlyEqual = isCharBeforeAndAfterPositionsRoughtlyEqual( | ||
| event.textEditor.document, | ||
| event.selections[0].anchor, | ||
| new Position(matchingTagPosition.line, matchingTagPosition.character) | ||
| ); | ||
|
|
||
| if (charBeforeAndAfterPositionsRoughtlyEqual) { | ||
| inMirrorMode = true; | ||
| const newCursor = new Selection( | ||
| matchingTagPosition.line, | ||
| matchingTagPosition.character, | ||
| matchingTagPosition.line, | ||
| matchingTagPosition.character | ||
| ); | ||
| window.activeTextEditor.selections = [...window.activeTextEditor.selections, newCursor]; | ||
| } | ||
| } | ||
| }).then(undefined, err => { | ||
| const msg = err.message ; | ||
| // mutes "rejected promise not handled within 1 second" | ||
| if (msg && !msg.endsWith('has been cancelled')){ | ||
| console.log(err); | ||
| } | ||
| return; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const exitMirrorMode = () => { | ||
| inMirrorMode = false; | ||
| window.activeTextEditor!.selections = [window.activeTextEditor!.selections[0]]; | ||
| }; | ||
|
|
||
| if (cursors.length === 2 && inMirrorMode) { | ||
| if (event.selections[0].isEmpty && event.selections[1].isEmpty) { | ||
| if ( | ||
| prevCursors.length === 2 && | ||
| event.selections[0].anchor.line !== prevCursors[0].anchor.line && | ||
| event.selections[1].anchor.line !== prevCursors[0].anchor.line | ||
| ) { | ||
| exitMirrorMode(); | ||
| return; | ||
| } | ||
|
|
||
| const charBeforeAndAfterPositionsRoughtlyEqual = isCharBeforeAndAfterPositionsRoughtlyEqual( | ||
| event.textEditor.document, | ||
| event.selections[0].anchor, | ||
| event.selections[1].anchor | ||
| ); | ||
|
|
||
| if (!charBeforeAndAfterPositionsRoughtlyEqual) { | ||
| exitMirrorMode(); | ||
| return; | ||
| } else { | ||
| // Need to cleanup in the case of <div |></div |> | ||
| if ( | ||
| shouldDoCleanupForHtmlAttributeInput( | ||
| event.textEditor.document, | ||
| event.selections[0].anchor, | ||
| event.selections[1].anchor | ||
| ) | ||
| ) { | ||
| const cleanupEdit = new WorkspaceEdit(); | ||
| const cleanupRange = new Range(event.selections[1].anchor.translate(0, -1), event.selections[1].anchor); | ||
| cleanupEdit.replace(event.textEditor.document.uri, cleanupRange, ''); | ||
| exitMirrorMode(); | ||
| workspace.applyEdit(cleanupEdit); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return Disposable.from(...disposables); | ||
| } | ||
|
|
||
| function getCharBefore(document: TextDocument, position: Position) { | ||
| const offset = document.offsetAt(position); | ||
| if (offset === 0) { | ||
| return ''; | ||
| } | ||
|
|
||
| return document.getText(new Range(document.positionAt(offset - 1), position)); | ||
| } | ||
|
|
||
| function getCharAfter(document: TextDocument, position: Position) { | ||
| const offset = document.offsetAt(position); | ||
| if (offset === document.getText().length) { | ||
| return ''; | ||
| } | ||
|
|
||
| return document.getText(new Range(position, document.positionAt(offset + 1))); | ||
| } | ||
|
|
||
| // Check if chars before and after the two positions are equal | ||
| // For the chars before, `<` and `/` are consiered equal to handle the case of `<|></|>` | ||
| function isCharBeforeAndAfterPositionsRoughtlyEqual(document: TextDocument, firstPos: Position, secondPos: Position) { | ||
| const charBeforePrimarySelection = getCharBefore(document, firstPos); | ||
| const charAfterPrimarySelection = getCharAfter(document, firstPos); | ||
| const charBeforeSecondarySelection = getCharBefore(document, secondPos); | ||
| const charAfterSecondarySelection = getCharAfter(document, secondPos); | ||
|
|
||
| /** | ||
| * Special case for exiting | ||
| * |<div> | ||
| * |</div> | ||
| */ | ||
| if ( | ||
| charBeforePrimarySelection === ' ' && | ||
| charBeforeSecondarySelection === ' ' && | ||
| charAfterPrimarySelection === '<' && | ||
| charAfterSecondarySelection === '<' | ||
| ) { | ||
| return false; | ||
| } | ||
| /** | ||
| * Special case for exiting | ||
| * | <div> | ||
| * | </div> | ||
| */ | ||
| if (charBeforePrimarySelection === '\n' && charBeforeSecondarySelection === '\n') { | ||
| return false; | ||
| } | ||
| /** | ||
| * Special case for exiting | ||
| * <div>| | ||
| * </div>| | ||
| */ | ||
| if (charAfterPrimarySelection === '\n' && charAfterSecondarySelection === '\n') { | ||
| return false; | ||
| } | ||
|
|
||
| // Exit mirror mode when cursor position no longer mirror | ||
| // Unless it's in the case of `<|></|>` | ||
| const charBeforeBothPositionRoughlyEqual = | ||
| charBeforePrimarySelection === charBeforeSecondarySelection || | ||
| (charBeforePrimarySelection === '/' && charBeforeSecondarySelection === '<') || | ||
| (charBeforeSecondarySelection === '/' && charBeforePrimarySelection === '<'); | ||
| const charAfterBothPositionRoughlyEqual = | ||
| charAfterPrimarySelection === charAfterSecondarySelection || | ||
| (charAfterPrimarySelection === ' ' && charAfterSecondarySelection === '>') || | ||
| (charAfterSecondarySelection === ' ' && charAfterPrimarySelection === '>'); | ||
|
|
||
| return charBeforeBothPositionRoughlyEqual && charAfterBothPositionRoughlyEqual; | ||
| } | ||
|
|
||
| function shouldDoCleanupForHtmlAttributeInput(document: TextDocument, firstPos: Position, secondPos: Position) { | ||
| // Need to cleanup in the case of <div |></div |> | ||
| const charBeforePrimarySelection = getCharBefore(document, firstPos); | ||
| const charAfterPrimarySelection = getCharAfter(document, firstPos); | ||
| const charBeforeSecondarySelection = getCharBefore(document, secondPos); | ||
| const charAfterSecondarySelection = getCharAfter(document, secondPos); | ||
|
|
||
| const primaryBeforeSecondary = document.offsetAt(firstPos) < document.offsetAt(secondPos); | ||
|
|
||
| /** | ||
| * Check two cases | ||
| * <div |></div > | ||
| * <div | id="a"></div > | ||
| * Before 1st cursor: ` ` | ||
| * After 1st cursor: `>` or ` ` | ||
| * Before 2nd cursor: ` ` | ||
| * After 2nd cursor: `>` | ||
| */ | ||
| return ( | ||
| primaryBeforeSecondary && | ||
| charBeforePrimarySelection === ' ' && | ||
| (charAfterPrimarySelection === '>' || charAfterPrimarySelection === ' ') && | ||
| charBeforeSecondarySelection === ' ' && | ||
| charAfterSecondarySelection === '>' | ||
| ); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about using the same name than vscode
mirrorCursorOnMatchingTag?