shinyypig / matlab-in-vscode

A vscode extension for matlab.
MIT License
45 stars 4 forks source link

大佬,可以改进一下查看变量的窗口么 #43

Closed cat-my-earn closed 3 months ago

cat-my-earn commented 3 months ago

我实现了在查看变量的窗口点击然后弹出变量内容,但是是使用openvar命令实现的。

我在拓展生成的js文件中修改了这两个函数,这个是javascript,大佬用的是ts的话可能还要稍微修改一下。 第一个函数:生成html的函数。第一个函数里面我还加了一点css,是一个点击动画,如果不需要的话可以去掉。主要是webview向拓展发送信息。

    async function a(e, t) {
      return o.existsSync(e)
        ? await (function (e) {
            return new Promise((t, s) => {
              let i = `
        <style>
        body {
            display: flex;
            justify-content: center;
        }

        table {
            font-size: 14px;
            width: auto;
            table-layout: auto;
            border-collapse: collapse;
        }

        td, th {
            border: 1px solid gray;
            padding: 5px;
            text-align: left;
        }

        td:active {
            background-color: red;
            color: green;
        }
        </style>

        <script>
        const vscode = acquireVsCodeApi();
        function handleClick(e) {
          vscode.postMessage({command: "openvar", text: e.innerText});
        }
        </script>
        <body>
        <table>
        </body>
        `;
              o.createReadStream(e)
                .pipe(r())
                .on("headers", (e) => {
                  i += "<tr>";
                  e.forEach((e) => {
                    i += `<th>${e}</th>`;
                  });
                  i += "</tr>";
                })
                .on("data", (e) => {
                  i += "<tr>";
                  for (let t in e)
                    i += `<td onclick='handleClick(this)'>${e[t]}</td>`;
                  i += "</tr>";
                })
                .on("end", () => {
                  i += "</table>";
                  o.unlink(e, (e) => {
                    e ? s(e) : t(i);
                  });
                })
                .on("error", (e) => {
                  s(e);
                });
            });
          })(e)
        : (await new Promise((e) => setTimeout(e, t)), await a(e, t));
    }

第二个函数:创建webview的函数,在创建webview之后随即定义了一个接收webview信息的函数,接收信息之后就用openvar打开变量窗口。如果不想额外新增窗口的话,可以用命令行读取变量内容之后刷新webview里面的内容,vscode向webview传递数据应该也好实现,然后用一个js函数更新表格里面的值就可以了。

        C = t.commands.registerCommand("matlab-in-vscode.variable", () => {
          !(function () {
            let e = h();
            if (e !== undefined) {
              if (!s) {
                s = t.window.createWebviewPanel(
                  "matlabScope",
                  "Matlab Variable Scope",
                  t.ViewColumn.Beside,
                  { enableScripts: true }
                );
                s.onDidDispose(() => {
                  s = undefined;
                });

                // 添加 onDidReceiveMessage 事件监听器
                s.webview.onDidReceiveMessage((message) => {
                  if (message.command === "openvar") {
                    let command = `openvar("${message.text}")`;
                    e.sendText(command);
                  }
                });
              } else {
                s.reveal();
              }
              e.sendText("variable_info;");
              f();
            } else {
              e = m();
            }
          })();
        });
shinyypig commented 3 months ago

我都不知道有 openvar 这个函数,加上之后很好用,感谢!

等最近多测试测试,没问题可以发布一个新版本。