cogentcore / cogent

A powerful, fast, elegant software ecosystem of apps for all platforms.
https://cogentcore.org/cogent
BSD 3-Clause "New" or "Revised" License
192 stars 10 forks source link

Automatically identify whether the AI prompt content is a string or a code #319

Closed ddkwork closed 7 months ago

ddkwork commented 8 months ago

Describe the feature

In addition, we need to use MD rendering and highlight syntax when the input prompt content is a block of code, and we need a scanning algorithm to automatically identify which programming language it is in order to set the highlight color

Relevant code

No response

kkoreilly commented 8 months ago

Yes, I will implement good MD code highlighting and rendering soon.

ddkwork commented 8 months ago

package main

import (
    "fmt"
    "regexp"
)

func extractCodeBlocks(text string) {
    patterns := map[string]string{
        "Go":     `\bfunc\b|\bpackage\b|\bimport\b`,
        "C++":    `\bclass\b|\bint\b|\bvoid\b`,
        "Python": `\bdef\b|\bimport\b|\bfrom\b`,
        "Rust":   `\bfn\b|\buse\b|\bmod\b`,
    }

    for lang, pattern := range patterns {
        re := regexp.MustCompile(pattern)
        matches := re.FindAllStringIndex(text, -1)
        if len(matches) > 0 {
            fmt.Printf("%s code blocks found:\n", lang)
            for _, match := range matches {
                fmt.Printf("Start line: %d, End line: %d\n", getLineNumber(text, match[0]), getLineNumber(text, match[1]))
            }
        }
    }
}

func getLineNumber(text string, index int) int {
    lines := 1
    for i := 0; i < index; i++ {
        if text[i] == '\n' {
            lines++
        }
    }
    return lines
}

func main() {
    text := `
        package main

        import (
            "fmt"
        )

        func main() {
            fmt.Println("Hello, world!")
        }

        // C++ code block
        class MyClass {
            int val;
        };

        # Python code block
        def hello():
            print("Hello, world!")

        // Rust code block
        pub fn hello() {
            println!("Hello, world!");
        }
    `
    extractCodeBlocks(text)
}
kkoreilly commented 8 months ago

Just to clarify, the issue is not that we need to magically detect the language of the code blocks. It tells us the language of the code blocks, and I just need to connect our markdown renderer with our syntax highlighter to achieve the appropriate highlighting.