问题部分中的VSCODE扩展堆叠输出

发布于 2025-02-12 21:38:48 字数 3344 浏览 0 评论 0 原文

有人可以给我一个提示,为什么vScode多次运行时,Vscode在问题部分中堆放了输出(在我们的自制作Vscode扩展程序中,来自同一文本文件)?

类定义:

import {  Uri, languages, Range, Diagnostic, Position, DiagnosticCollection } from "vscode";

export class ProblemHelper{

   public rangeObj: Range | undefined; 
   public  problemObj: Diagnostic | undefined;
   public problemEntries: Array<Diagnostic> = [];

   //public static diagnosticCollection: DiagnosticCollection;
   constructor(actDocument: Uri| undefined,message: string, severity: number){

     console.log("problemhelper const 0 " + this.problemEntries.length);
     this.rangeObj = new Range(new Position(1,1),new Position(1,1) );
     this.problemObj = new Diagnostic(this.rangeObj,message,severity );
     this.problemObj.source = actDocument?.fsPath;
     this.problemEntries.push(this.problemObj);
     console.log("problemhelper const 1 " + this.problemEntries.length);      
  }

  public  setProblems(actDocument: Uri, coll: DiagnosticCollection, mess: string, sev: number) {
      this.problemEntries.forEach(e => {
          if (this.problemObj?.message === e.message) { 
             return;
          }
      });
      this.rangeObj = new Range(new Position(1,1),new Position(1,1) );
      this.problemObj = new Diagnostic(this.rangeObj,mess,sev );
      this.problemEntries.push(this.problemObj);
      console.log("problemhelper const 3 "  + this.problemEntries.length);
      coll.set(actDocument,this.problemEntries);
      console.log("problemhelper const 4 " + coll.get(actDocument)?.length);
      //ProblemHelper.diagnosticCollection!.set(actDocument,ProblemHelper.problemEntries);
  }

  public deleteProblems(actDocument: Uri | undefined, coll: DiagnosticCollection) {
       console.log("in delete before " + this.problemEntries.length);
       coll.delete(actDocument!);
       this.problemEntries = [];
       console.log("in delete after " + this.problemEntries.length);
       //coll.dispose();
   }

}

与:

const diagnosticCollection = languages.createDiagnosticCollection(window.activeTextEditor?.document.uri.fsPath);
let temp = new ProblemHelper(window.activeTextEditor?.document.uri,"test", 0);
temp.deleteProblems(window.activeTextEditor?.document.uri,diagnosticCollection);

temp.setProblems(window.activeTextEditor?.document.uri!,diagnosticCollection,"test1",1);
    
temp.setProblems(window.activeTextEditor?.document.uri!,diagnosticCollection,"test222",2);
console.log("end");

在调试控制台中输出(屏幕截图)来自“扩展开发主机”,可以看出正确的条目数量,但每次都会堆叠起来):

problemhelper const 0 0
problemhelper const 1 1
in delete before 1
in delete after 0
problemhelper const 3 1
problemhelper const 4 1
problemhelper const 3 2
problemhelper const 4 2
end
problemhelper const 0 0
problemhelper const 1 1
in delete before 1
in delete after 0
problemhelper const 3 1
problemhelper const 4 1
problemhelper const 3 2
problemhelper const 4 2
end

VSCODE版本:1.66.0(Win 10)

谢谢!

Can someone give me a hint why vscode is stacking the outputs in the problems section when ran multiple times (in our self made vscode extension, from the same textfile)?

first run

second run

class definition:

import {  Uri, languages, Range, Diagnostic, Position, DiagnosticCollection } from "vscode";

export class ProblemHelper{

   public rangeObj: Range | undefined; 
   public  problemObj: Diagnostic | undefined;
   public problemEntries: Array<Diagnostic> = [];

   //public static diagnosticCollection: DiagnosticCollection;
   constructor(actDocument: Uri| undefined,message: string, severity: number){

     console.log("problemhelper const 0 " + this.problemEntries.length);
     this.rangeObj = new Range(new Position(1,1),new Position(1,1) );
     this.problemObj = new Diagnostic(this.rangeObj,message,severity );
     this.problemObj.source = actDocument?.fsPath;
     this.problemEntries.push(this.problemObj);
     console.log("problemhelper const 1 " + this.problemEntries.length);      
  }

  public  setProblems(actDocument: Uri, coll: DiagnosticCollection, mess: string, sev: number) {
      this.problemEntries.forEach(e => {
          if (this.problemObj?.message === e.message) { 
             return;
          }
      });
      this.rangeObj = new Range(new Position(1,1),new Position(1,1) );
      this.problemObj = new Diagnostic(this.rangeObj,mess,sev );
      this.problemEntries.push(this.problemObj);
      console.log("problemhelper const 3 "  + this.problemEntries.length);
      coll.set(actDocument,this.problemEntries);
      console.log("problemhelper const 4 " + coll.get(actDocument)?.length);
      //ProblemHelper.diagnosticCollection!.set(actDocument,ProblemHelper.problemEntries);
  }

  public deleteProblems(actDocument: Uri | undefined, coll: DiagnosticCollection) {
       console.log("in delete before " + this.problemEntries.length);
       coll.delete(actDocument!);
       this.problemEntries = [];
       console.log("in delete after " + this.problemEntries.length);
       //coll.dispose();
   }

}

called with:

const diagnosticCollection = languages.createDiagnosticCollection(window.activeTextEditor?.document.uri.fsPath);
let temp = new ProblemHelper(window.activeTextEditor?.document.uri,"test", 0);
temp.deleteProblems(window.activeTextEditor?.document.uri,diagnosticCollection);

temp.setProblems(window.activeTextEditor?.document.uri!,diagnosticCollection,"test1",1);
    
temp.setProblems(window.activeTextEditor?.document.uri!,diagnosticCollection,"test222",2);
console.log("end");

Output in debug console (screenshots are from "EXTENSION DEVELOPMENT HOST", as can be seen the correct amount of entries is printed out, but it is getting stacked up every time):

problemhelper const 0 0
problemhelper const 1 1
in delete before 1
in delete after 0
problemhelper const 3 1
problemhelper const 4 1
problemhelper const 3 2
problemhelper const 4 2
end
problemhelper const 0 0
problemhelper const 1 1
in delete before 1
in delete after 0
problemhelper const 3 1
problemhelper const 4 1
problemhelper const 3 2
problemhelper const 4 2
end

VSCODE Version: 1.66.0 (WIN 10)

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事和酒 2025-02-19 21:38:48

非常感谢您,您是对的,我完成了两次新课程,所以我在Extension Startup上运行此课程,然后Do clear()而不是删除(xx)。现在我可以前进。

Thank you very much you are right, i made a new class twice, so i run this at extension startup, and do clear() instead of delete(xx). Now i can move forward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文