Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-language detection and syntax highlighting not working when language is not explicitly specified #26

Open
knlshh opened this issue May 11, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@knlshh
Copy link

knlshh commented May 11, 2023

Hello,

I see this code in CodeAttributedString of Highlightr. It has a non-nil check for language else it doesn't proceed and hence doesn't highlight nor auto-detect the language. Is this expected?

    func highlight(_ range: NSRange)
    {
        if(language == nil)
        {
            return;
        }
        
        if let highlightDelegate = highlightDelegate
        {
            let shouldHighlight : Bool? = highlightDelegate.shouldHighlight?(range)
            if(shouldHighlight != nil && !shouldHighlight!)
            {
                return;
            }
        }

        
        let string = (self.string as NSString)
        let line = string.substring(with: range)
        DispatchQueue.global().async
        {
            let tmpStrg = self.highlightr.highlight(line, as: self.language!)
            DispatchQueue.main.async(execute: {
                //Checks to see if this highlighting is still valid.
                if((range.location + range.length) > self.stringStorage.length)
                {
                    self.highlightDelegate?.didHighlight?(range, success: false)
                    return;
                }
                
                if(tmpStrg?.string != self.stringStorage.attributedSubstring(from: range).string)
                {
                    self.highlightDelegate?.didHighlight?(range, success: false)
                    return;
                }
                
                self.beginEditing()
                tmpStrg?.enumerateAttributes(in: NSMakeRange(0, (tmpStrg?.length)!), options: [], using: { (attrs, locRange, stop) in
                    var fixedRange = NSMakeRange(range.location+locRange.location, locRange.length)
                    fixedRange.length = (fixedRange.location + fixedRange.length < string.length) ? fixedRange.length : string.length-fixedRange.location
                    fixedRange.length = (fixedRange.length >= 0) ? fixedRange.length : 0
                    self.stringStorage.setAttributes(attrs, range: fixedRange)
                })
                self.endEditing()
                self.edited(TextStorageEditActions.editedAttributes, range: range, changeInLength: 0)
                self.highlightDelegate?.didHighlight?(range, success: true)
            })
            
        }
        
    }
@helje5
Copy link
Member

helje5 commented May 11, 2023

I wouldn't say it is intentional (rather a missing feature), but it can't highlight if it doesn't know what language to apply.
And I don't know how to detect a language, does Highlightr have sth for this?

@helje5 helje5 added the enhancement New feature or request label May 11, 2023
@knlshh
Copy link
Author

knlshh commented May 11, 2023

The initializer for CodeEditor says that Highlightr will auto-detect the language if one is not provided.

  /**
   * Configures a read-only CodeEditor View with the given parameters.
   *
   * - Parameters:
   *   - source:      A String that holds the source code to be displayed.
   *   - language:    Optionally set a language (e.g. `.swift`), otherwise
   *                  Highlight.js will attempt to detect the language.
   *   - theme:       The name of the theme to use, defaults to "pojoaque".
   *   - fontSize:    On macOS this Binding can be used to persist the size of
   *                  the font in use. At runtime this is combined with the
   *                  theme to produce the full font information. (optional)
   *   - flags:       Configure whether the text is selectable
   *                  (defaults to both).
   *   - indentStyle: Optionally insert a configurable amount of spaces if the
   *                  user hits "tab".
   *   - autoPairs:   A mapping of open/close characters, where the close
   *                  characters are automatically injected when the user enters
   *                  the opening character. For example: `[ "{": "}" ]` would
   *                  automatically insert the closing "}" if the user enters
   *                  "{". If no value is given, the default mapping for the
   *                  language is used.
   *   - inset:       The editor can be inset in the scroll view. Defaults to
   *                  8/8.
   */

@helje5
Copy link
Member

helje5 commented May 11, 2023

Interesting. Did you try to remove the check and see whether it works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants