Skip to content

API: Expose decorations stickiness to extensions #15758

Closed

Description

@CoenraadS
  • VSCode Version: 1.7.1
  • OS Version: Windows 10 14965

Steps to Reproduce:

  1. Create a text decoration that is applied to where the cursor is (easy example is decoration at line 0, char 0-1 on an empty document)
  2. Start typing
  3. Decorator is applied correctly to first letter
  4. Delete all text
  5. Type again, decorator is applied to everything instead of just first letter

https://gfycat.com/InferiorSlimHerring

'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

    var activeEditor = vscode.window.activeTextEditor;
    if (activeEditor) {
        updateDecorations(activeEditor);
    }

    vscode.window.onDidChangeActiveTextEditor(editor => {
        activeEditor = editor;
        if (editor) {
            updateDecorations(activeEditor);
        }
    }, null, context.subscriptions);

    vscode.workspace.onDidChangeTextDocument(event => {
        if (activeEditor && event.document === activeEditor.document) {
            updateDecorations(activeEditor);
        }
    }, null, context.subscriptions);
    vscode.workspace.onDidChangeTextDocument(event => {
        if (activeEditor && event.document === activeEditor.document) {
            updateDecorations(activeEditor);
        }
    }, null, context.subscriptions)
}

function updateDecorations(activeEditor : vscode.TextEditor) {
    if (!activeEditor) {
        return;
    }

    let firstLetterDecoration = vscode.window.createTextEditorDecorationType({color: "#ff0000"});

    let range = new vscode.Range(new vscode.Position(0,0), new vscode.Position(0,1));

    activeEditor.setDecorations(firstLetterDecoration, [range]);
}

// this method is called when your extension is deactivated
export function deactivate() {
}

Activity

  1. changed the title [-]setDecorations Range Cursor interaction[/-] [+]setDecorations Range new text interaction[/+] on Nov 19, 2016
  2. alexdima commented on Nov 30, 2016

    @alexdima
    Member

    CoenraadS

    1. Decorations "move" with the text

    If you add a decoration, say at a certain range such as [0,0 - 0,1], typing (inserting text) inside that range or on the edges of the range causes the decoration to grow.

    2. The code inserts very many decorations

    The sample you have provided, creates a new decoration editor type on each keystroke and sets that type to a new decoration on each keystroke (i.e. N keystrokes => N decoration types each with one decoration), meanwhile they never get removed. i.e. move createTextEditorDecorationType to the activate method or in a static context or dispose the decoration type when you are no longer wanting it.

    i.e.
    image

  3. added
    *question
    on Nov 30, 2016
  4. CoenraadS commented on Dec 4, 2016

    @CoenraadS
    ContributorAuthor

    Would it be possible to add stickiness as a decoration option? I see something already exists that is similar.

    let options: editorCommon.IModelDecorationOptions = {
    	stickiness: editorCommon.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,
    	className: 'bracket-match'
    	};
    
  5. changed the title [-]setDecorations Range new text interaction[/-] [+]Expose decorations stickiness to extensions[/+] on Dec 13, 2016
  6. added
    feature-request
    and removed
    *question
    on Dec 13, 2016
  7. 1 remaining item

  8. CoenraadS commented on May 2, 2017

    @CoenraadS
    ContributorAuthor

    Would a PR for this be accepted? And if so would it be possible for a beginner?

  9. CoenraadS commented on May 16, 2017

    @CoenraadS
    ContributorAuthor

    alexandrudima

    Do you have time to take a look at this? A PR is ready.

  10. added a commit that references this issue on May 30, 2017
  11. added
    verification-needed
    on May 30, 2017
  12. alexdima commented on May 30, 2017

    @alexdima
    Member

    e.g. for verification:

    	var oo = vscode.window.createTextEditorDecorationType({
    		backgroundColor: 'red',
    		rangeBehavior: vscode.DecorationRangeBehavior.OpenOpen
    	});
    	var cc = vscode.window.createTextEditorDecorationType({
    		backgroundColor: 'blue',
    		rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed
    	});
    	var oc = vscode.window.createTextEditorDecorationType({
    		backgroundColor: 'green',
    		rangeBehavior: vscode.DecorationRangeBehavior.OpenClosed
    	});
    	var co = vscode.window.createTextEditorDecorationType({
    		backgroundColor: 'yellow',
    		rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen
    	});
    
    	vscode.window.activeTextEditor.setDecorations(oo, [new vscode.Range(9, 7, 9, 10)]);
    	vscode.window.activeTextEditor.setDecorations(cc, [new vscode.Range(10, 7, 10, 10)]);
    	vscode.window.activeTextEditor.setDecorations(oc, [new vscode.Range(11, 7, 11, 10)]);
    	vscode.window.activeTextEditor.setDecorations(co, [new vscode.Range(12, 7, 12, 10)]);
    
  13. added
    verified
    and removed
    verification-needed
    on Jun 1, 2017
  14. locked and limited conversation to collaborators on Nov 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    apifeature-requestverified

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions