Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

84
Vistas
Develop vs code extension to edit text

I want to build an extension that edits all the links in a file in the editor. If should be something like this

BEFORE:

<img src="assets/images/avatars/avatar-1.jpg" alt="">
<link rel="stylesheet" href="assets/css/icons.css">
<link rel="stylesheet" href="assets/css/uikit.css">
<link rel="stylesheet" href="assets/css/style.css"> 

AFTER:

<img src="{% static 'assets/images/avatars/avatar-1.jpg' %}" alt="">
<link rel="stylesheet" href="{% static 'assets/css/icons.css' %}">
<link rel="stylesheet" href="{% static 'assets/css/uikit.css' %}">
<link rel="stylesheet" href="{% static 'assets/css/style.css' %}"> 

This is the code of my extension.js file so far

const vscode = require('vscode');

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

    let disposable = vscode.commands.registerCommand('auto-django.autoDjango', function () {

        const editor = vscode.window.activeTextEditor
        const selection = editor.selection
        const text = editor.document.getText(selection)

        var regexp = /<img[^>]+src\s*=\s*['"]([^'"]+)['"][^>]*>/g;

        var match = regexp.exec(text);
        var src = match[1];

        const newText = `{% static '${src}' %}`

        editor.edit(builder => builder.replace(selection, newText))
    });

    context.subscriptions.push(disposable);
}

And all this code does is that it takes the first image src link and changes it to the edited link, that is good, but it also replaces it with the whole text in the editor.

But what i want to do is that it should just replace with each of the links in the editor.

Any help will be appreciated. Thanks

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

First thing I see:

editor.document.getText(some Range) takes a Range not a Selection - that is why it is returning all the document text - it does that when the argument is invalid.

editor.document.getText(new vscode.Range(selection.start, selection.end))

should get you the text of the selection.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda