Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

post code load function #15

Open
akshit-sharma opened this issue Jul 7, 2023 · 6 comments
Open

post code load function #15

akshit-sharma opened this issue Jul 7, 2023 · 6 comments

Comments

@akshit-sharma
Copy link

akshit-sharma commented Jul 7, 2023

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
Copy link
Owner

Dhanus3133 commented Jul 16, 2023

  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
Copy link
Author

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
Copy link
Owner

Dhanus3133 commented Jul 20, 2023

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants