0

그래서 기본적으로 현재 줄 값을 가져 와서 구문 분석하고 px를 rem으로 바꾸는 시각적 인 스튜디오 코드 확장을 만들려고합니다. (나중에이 변수를 만들었습니다. 기본 및 단위를 수정할 때)Visual Studio 코드 확장 만들기, 3 질문

강조 표시된 값을 얻는 방법은 Microsoft API 사이트에서 볼 수 있습니다. 먼저 함수를 먼저 가져오고 싶다는 생각이 들었습니다.

그런 다음 내 코드와 나는 라인이 1 개 이상의 값 예를 들어, 경우 최종 값으로 반환 할 수 있겠군요 오전

여백 : 22 픽셀 32px 0 32px;

다음은 코드입니다.

'use strict'; 
import * as vscode from 'vscode'; 
export function activate(context: vscode.ExtensionContext) { 
    let disposable = vscode.commands.registerCommand('extension.pxToEm',() => { 
     var base, editor, initBase, original, selection, text, unit, values, totalBase, position; 
     editor = vscode.window.activeTextEditor; 
     selection = editor.selection; 
     text = editor.document.getText(selection); 
     base = <any>16; 
     unit = 'rem'; 
     values = text.match(/([0-9]+)px/gi); 
     var returnValue = function(text) { 
      if (values !== null) { 
       values.forEach(function(val, key) { 
        text = text.replace(val, parseInt(val)/base + unit); 
        if (key > values.length - 1) { 
         totalBase = '/' + base.replace(/(\r\n|\n|\r)/gi, ''); 
         text = text.replace(totalBase, ' ').replace(/(\r\n|\n|\r)/gi, ''); 
         text = text + '\n'; 
        } 
       }); 
      } 
      return text; 
     }; 
     vscode.window.showInformationMessage(returnValue(text)); 
    }); 
    context.subscriptions.push(disposable); 
    } 
+0

이 제대로 작동 이제하지만 난 그냥 커서 현재 선으로 사용하고자하는 ... 내가 어떤이 없습니다 생각. 어떤 도움을 주시면 감사하겠습니다. (선택 항목 대신 현재 라인에서 내용을 가져 와서 반환 할 때 대체하십시오. –

답변

1

당신은 다음과 같이 선택 객체를 사용할 수 있습니다

내 코드를 업데이트 한
editor = vscode.window.activeTextEditor; 
selection = editor.selection; 
if(selection.isEmpty) 
{ 
    // the Position object gives you the line and character where the cursor is 
     const position = editor.selection.active; 
     var newPosition = position.with(position.line, 0); 
     var newSelection = new vscode.Selection(newPosition, newPosition); 
     editor.selection = newSelection; 
} 
text = editor.document.getText(selection);