vscode-leetcode icon indicating copy to clipboard operation
vscode-leetcode copied to clipboard

[FR] TypeScript support for leetcode's built-in data structures 力扣自定义数据结构(如ListNode、TreeNode)的TypeScript支持

Open DarrenDanielDay opened this issue 1 year ago • 5 comments

🚀 Feature Proposal

Add TypeScript type declaration file of leetcode's built-in data structures to the workspace root (such as ~/.leetcode) automatically when users open a leetcode workspace with language typescript.

Motivation

The online editor has correct type inference of leetcode's built-in data structure, such as TreeNode and ListNode. It's reasonable to have the same experience with this vscode extension.

Contribution

I think there already exists a TypeScript declaration file for those built-in data structures for the online editor. Just create a copy of that file via vscode.workspace.fs.writeFile when users open a leetcode workspace with language typescript. Also create a tsconfig.json file if necessary, or just execute npx tsc --init.

More

Support running JavaScript/TypeScript test cases locally, since nodejs 10+ is required for this extension, possibly by using babel to transpile TypeScript code to JavaScript code and calling nodejs API like vm.runInContext to execute the JavaScript code.

DarrenDanielDay avatar Aug 04 '22 17:08 DarrenDanielDay

I've made my solution for this feature.

Here is the solution repo: https://github.com/DarrenDanielDay/leetcode-typescript-workspace

I'm also trying to make it easier to use, with less extra code. Here is my new idea and PR. demo PR

But the simple parser is not necessary if this extension supports to analyze and run test cases natively! Programmers will play tricks to make software suitable for use until software really meets the user requirements. But it's really really really a waste of time!

DarrenDanielDay avatar Aug 28 '22 17:08 DarrenDanielDay

翻了一下issue,发现中国用户比较多,外加LeetCode-OpenSource组织位于中国上海,那我也就不强行用我这塑料英语交流了。

简单说下前面的Feature Proposal部分,就是对于习惯使用JavaScript/TypeScript解题的用户提供一些力扣内置数据结构的类型定义。比如ListNodeTreeNode这种,是由力扣自定义的class,线上编辑器有定义,但vscode里就没有还会爆红。可能的实现方法是直接在插件选的放代码的文件夹里直接生成几个类型定义文件,如果必要可以再加个tsconfig.json配置文件配上力扣线上判题的编译配置。然后如果这些都能内置集成,可以顺便附加一个基于nodejs的运行工具和调试配置,沙箱运行代码可以用vm.runInContext

为什么有这种想法呢?因为在线的编辑器里对TypeScript的支持是很好的,目前TypeScript也是线上编辑器里唯一一个能免费用到语法分析、代码提示、类型检查的完整功能的语言。既然免费线上编辑器让咱爽了,为啥免费插件就没了呢?

然后这个issue迟迟没有人回应,于是我做了自己的解决方案,代码仓库是这个: https://github.com/DarrenDanielDay/leetcode-typescript-workspace

然后我自己用得还是不太爽,今天又搞了个解析示例测试用例的parser,写了demo还给自己提了PR: demo PR

但是这parser显然没什么必要,力扣官方肯定有更好的解析用例输入和输出的方法。作为程序员,软件用不爽就会想去hack一些自定义的东西,直到软件本身真正满足了需求。我这么搞真的很浪费时间,希望官方能有更好的方案。

DarrenDanielDay avatar Aug 28 '22 18:08 DarrenDanielDay

多造一点假的回复是不是就会有人来看呢?

DarrenDanielDay avatar Aug 28 '22 18:08 DarrenDanielDay

感觉这个 repo 是不是没人维护了...快一年没有 commit 了, pr 和 issue 也有一堆 open 的

NotEvenANeko avatar Mar 21 '23 08:03 NotEvenANeko

I am having this issue in Java language too. VSCode is not able to recognize the ListNode data structure built in and fails to provide basic features like code formatting and suggestions for the code.

theashggl avatar Jul 10 '23 08:07 theashggl