Dhanus3133 / Leetbuddy.nvim

Solve Leetcode problems within Neovim 🔥
MIT License
138 stars 15 forks source link

post code load function #15

Open akshit-sharma opened 1 year ago

akshit-sharma commented 1 year ago

Custom callback function execution after loading the any question from LBQuestions on the code buffer. Use case 1: user can resolve the empty code buffer problem. The custom callback function can detect if buffer is empty and call :LBReset automatically. Use case 2: by default the lsp on cpp gives errors. Leetcode assume using namespace std but doesn't show that in the code. A user can parse the text and return a new code string with some changes such as string -> std::string, vector -> std::vector and so on (including appropriate header files and ).

For the both the use cases, a post code load callback can give user flexibility to change the code according to how he/she wants.

Dhanus3133 commented 1 year ago
  1. Will update with this.
  2. Leetcode does only give the template without any headers/imports. You can just add the headers in the top of the file fixes LSP warnings easily. It can be too complex or impossible for mapping and replacing for each methods and it can be varying for other languages too.

Let me know if there is any other possible way.

akshit-sharma commented 1 year ago

I agree, 2nd use case will be too complex and hard to implement as a library maintainer for each method and varied languages.

I was looking for a flexibility of having a setting for callback function as a

require('leetbuddy').setup({
  post_code_buffer_callback = CppCodeFixer
})

CppCodeFixer being a function by the plugin user.

post_code_buffer_callback(codeBuffer) is called after buffer is initialed.

This gives plugin user an ability to modify the codeBuffer and return a string. As a plugin user, I can make a function for 2nd use case and supply it to the leetbuddy by setting post_code_buffer_callback (or whatever the appropriate name is).

This way, the library maintainer doesn't have to factor in all the mapping and the user, if they want can make their own function to modify the buffer initialization.

Does this flexibility in leetbuddy seems possible to you?

Dhanus3133 commented 1 year ago

So, lets say if you're going to you use cpp then adding these code

#include <bits/stdc++.h>
using namespace std;

at the beginning when running LBReset command. I guess this can solve most of the LSP errors. And in the post_code_buffer_callback (mentioned) what actions will the user performs? Can you give me some more examples even when using the headers.

akshit-sharma commented 1 year ago

I was hoping to parse through the code buffer. Example Code 1:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {

    }
};

The CppCodeFixer function can see that string is used. So, I can modify string to std::string and insert an import statement #include <string>. I would prefer using std::string over string and having std declared in global. The function can return something like:

#include <string>

class Solution {
public:
    int lengthOfLongestSubstring(std::string s) {

    }
};

The CppCodeFixer function can also be used to modify code with Linked list or trees as well. Example Code 2:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {

    }
};

User can hard code the function to uncomment the comments for the definition of struct ListNode and return something like:

/**
 * Definition for singly-linked list.
 */
 struct ListNode {
      int val;
     ListNode *next;
     ListNode() : val(0), next(nullptr) {}
     ListNode(int x) : val(x), next(nullptr) {}
     ListNode(int x, ListNode *next) : val(x), next(next) {}
 };
 /*
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {

    }
};

Technically, the user can do this manually everytime the new code buffer opens. I am just trying to automate them.

Dhanus3133 commented 1 year ago

Instead of allowing the user to hard code the function for parsing the code, better to make leetbuddy automatically does this by default. Cause this can benefit all. If you're interested in doing this for cpp open up a PR :)

akshit-sharma commented 1 year ago

Right now, I have a lot of things on my plate. I will send a PR if i am able to finish it. I might take longer as i am not that familiar with lua and neovim plugin dev. To anyone else, feel free to work on this issue if you have an idea how to accomplish the same.