As a copy-and-paste engineer with many years of experience, I think it is a very real requirement for business development to complete new logic by referring to existing codes. If the corresponding code segment can be quickly called through fuzzy memory instead of manual global search, the efficiency of code input can be improved to a certain extent.

Based on this idea, a vscode plug-in Code Finder has been developed. Welcome to use it.

Use effect

In an analogy to the Smart pinyin input method in Chinese, we type in a phrase and find the most commonly used phrase just by looking at the first letter.

If you type code this way, it looks like this

CodeFinder is an intelligent input method for code that uses a small number of characters to quickly complete one or more lines of code. Automatically extracts all snippets of the current project, or you can add custom snippets. Support for all programming languages, such as Javascript, HTML, CSS, Python, Go, PHP, Vue, React…

Implementation of some core logic

  • Fragmentation of code snippets

Most of the code snippets are from local projects, so snippet extraction of the project source code is required. At first, I tried to segment it from syntax and semantics, but I found it was a one-way street. For most programming languages, we use indentation to express paragraphs of code. We can simply divide paragraphs by indentation. Define a simple rule that the number of prefix Spaces indicates the hierarchy, and that lower-level lines of code can form paragraphs with higher-level lines of code. Sibling lines of code should belong to different snippets. Then you can recursively partition to get each snippet of code. Reaggregate the same code snippet.

  • Indexing and searching

Essentially, it’s a search problem. For input, you can use a Trie tree to index individual code snippets. Because the search takes tens of milliseconds to complete locally, it may become impossible to traverse once the code fragment reaches a certain magnitude, so pruning logic is introduced to discard poorly matched branches of breadth traversal when time runs out.

  • Skip input

To reduce interference with normal input, distinguish between regular input and calls to input methods. That’s why “skip typing” was created: multiple words entered in succession separated by Spaces, ignoring other symbols. If the input is omitted, it is initiated by the user. Otherwise, it is considered as common input. Skip input is prompted every time, whereas regular input is only prompted if there is a good match.