Skip to main content

code

                            
                              
                                import
                                
                                {
                                ...
                                }
                                
                                from
                                
                                "@motion-canvas/2d/lib/code"
                                ;
                                

Classes

  • CodeSignalContext
  • LezerHighlighter

Interfaces

  • CodeFragment
  • CodeFragmentDrawingInfo
  • CodeHighlighter
  • CodeScope
  • CodeSignalHelpers
  • HighlightResult
  • RawCodeFragment

Type Aliases

CodeDiffer

                            
                              
                                type 
                                CodeDiffer
                                 = 
                                
                                  
                                    
                                      from
                                      : 
                                      CodeScope
                                    
                                    
                                      to
                                      : 
                                      CodeScope
                                    
                                    
                                      tokenize
                                      : 
                                      CodeTokenizer
                                    
                                  
                                
                                 => 
                                CodeTag
                                []
                              
                              

CodePoint

                            
                              
                                type 
                                CodePoint
                                 = 
                                
                                  
                                    
                                      number
                                    
                                    
                                      number
                                    
                                  
                                
                              
                              

CodeRange

                            
                              
                                type 
                                CodeRange
                                 = 
                                
                                  
                                    
                                      CodePoint
                                    
                                    
                                      CodePoint
                                    
                                  
                                
                              
                              

CodeSelection

                            
                              
                                type 
                                CodeSelection
                                 = 
                                CodeRange
                                []
                              
                              

CodeSignal

                            
                              
                                type 
                                CodeSignal
                                
                                  
                                    
                                      TOwner
                                    
                                  
                                
                                 = 
                                
                                  
                                    
                                      Signal
                                      
                                        
                                          
                                            PossibleCodeScope
                                          
                                          
                                            CodeScope
                                          
                                          
                                            TOwner
                                          
                                          
                                            CodeSignalContext
                                            
                                              
                                                
                                                  TOwner
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                    
                                      CodeSignalHelpers
                                      
                                        
                                          
                                            TOwner
                                          
                                        
                                      
                                    
                                  
                                
                              
                              

Type Parameters


CodeTag

                            
                              
                                type 
                                CodeTag
                                 = 
                                SignalValue
                                
                                  
                                    
                                      
                                        
                                          
                                            PossibleCodeFragment
                                          
                                          
                                            CodeScope
                                          
                                          
                                            CodeTag
                                            []
                                          
                                        
                                      
                                    
                                  
                                
                              
                              

CodeTokenizer

                            
                              
                                type 
                                CodeTokenizer
                                 = 
                                
                                  
                                    
                                      input
                                      : 
                                      string
                                    
                                  
                                
                                 => 
                                string
                                []
                              
                              

PossibleCodeFragment

                            
                              
                                type 
                                PossibleCodeFragment
                                 = 
                                
                                  
                                    
                                      CodeFragment
                                    
                                    
                                      CodeMetrics
                                    
                                    
                                      RawCodeFragment
                                    
                                    
                                      string
                                    
                                  
                                
                              
                              

PossibleCodeScope

                            
                              
                                type 
                                PossibleCodeScope
                                 = 
                                
                                  
                                    
                                      CodeScope
                                    
                                    
                                      CodeTag
                                      []
                                    
                                    
                                      string
                                    
                                  
                                
                              
                              

PossibleCodeSelection

                            
                              
                                type 
                                PossibleCodeSelection
                                 = 
                                
                                  
                                    
                                      CodeRange
                                    
                                    
                                      CodeRange
                                      []
                                    
                                  
                                
                              
                              

Variables

DefaultHighlightStyle

                            
                              
                                const 
                                DefaultHighlightStyle
                                : 
                                HighlightStyle
                                 = 
                                ...
                              
                              

Functions

CODE

                            
                              
                                public 
                                CODE
                                
                                  
                                    
                                      strings
                                      : 
                                      TemplateStringsArray
                                    
                                    
                                      ...
                                      tags
                                      : 
                                      CodeTag
                                      []
                                    
                                  
                                
                                : 
                                CodeTag
                                []
                              
                              

Parameters


codeSignal

                            
                              
                                public 
                                codeSignal
                                (): 
                                PropertyDecorator
                              
                              

consolidateCodeRanges

                            
                              
                                public 
                                consolidateCodeRanges
                                
                                  
                                    
                                      ranges
                                      : 
                                      CodeRange
                                      []
                                    
                                  
                                
                                : 
                                CodeRange
                                []
                              
                              

Parameters


defaultDiffer

                            
                              
                                public 
                                defaultDiffer
                                
                                  
                                    
                                      from
                                      : 
                                      CodeScope
                                    
                                    
                                      to
                                      : 
                                      CodeScope
                                    
                                    
                                      tokenize
                                      : 
                                      CodeTokenizer
                                    
                                  
                                
                                : 
                                CodeTag
                                []
                              
                              

Default diffing function utilizing patienceDiff .

Parameters

  • from : CodeScope

    The original code scope.

  • to : CodeScope

    The new code scope.

  • tokenize : CodeTokenizer

    The inherited tokenizer to use.


defaultTokenize

                            
                              
                                public 
                                defaultTokenize
                                
                                  
                                    
                                      input
                                      : 
                                      string
                                    
                                  
                                
                                : 
                                string
                                []
                              
                              

Default tokenizer function used by ownerless code signals.

Parameters

  • input : string

    The code to tokenize.


extractRange

                            
                              
                                public 
                                extractRange
                                
                                  
                                    
                                      range
                                      : 
                                      CodeRange
                                    
                                    
                                      fragments
                                      : 
                                      CodeTag
                                      []
                                    
                                  
                                
                                : 
                                
                                  
                                    
                                      CodeTag
                                      []
                                    
                                    
                                      number
                                    
                                  
                                
                              
                              

Transform the fragments to isolate the given range into its own fragment.

This function will try to preserve the original fragments, resolving themonly if they overlap with the range.

Parameters


findAllCodeRanges

                            
                              
                                public 
                                findAllCodeRanges
                                
                                  
                                    
                                      code
                                      : 
                                      string
                                    
                                    
                                      pattern
                                      : 
                                      
                                        
                                          
                                            string
                                          
                                          
                                            RegExp
                                          
                                        
                                      
                                    
                                    
                                      limit
                                      : 
                                      number
                                       = 
                                      Infinity
                                    
                                  
                                
                                : 
                                CodeRange
                                []
                              
                              

Find all code ranges that match the given pattern.

Parameters

  • code : string

    The code to search in.

  • pattern : string RegExp

    Either a string or a regular expression to search for.

  • limit : number = Infinity

    An optional limit on the number of ranges to find.


insert

                            
                              
                                public 
                                insert
                                
                                  
                                    
                                      code
                                      : 
                                      string
                                    
                                  
                                
                                : 
                                RawCodeFragment
                              
                              

Create a code fragment that represents an insertion of code.

Can be used in conjunction with edit .

Parameters

  • code : string

    The code to insert.


inverseCodeRange

                            
                              
                                public 
                                inverseCodeRange
                                
                                  
                                    
                                      ranges
                                      : 
                                      CodeRange
                                      []
                                    
                                  
                                
                                : 
                                CodeRange
                                []
                              
                              

Parameters


isCodeRange

                            
                              
                                public 
                                isCodeRange
                                
                                  
                                    
                                      value
                                      : 
                                      unknown
                                    
                                  
                                
                                : 
                                value 
                                is 
                                CodeRange
                              
                              

Parameters


isCodeScope

                            
                              
                                public 
                                isCodeScope
                                
                                  
                                    
                                      value
                                      : 
                                      any
                                    
                                  
                                
                                : 
                                value 
                                is 
                                CodeScope
                              
                              

Parameters


isPointInCodeRange

                            
                              
                                public 
                                isPointInCodeRange
                                
                                  
                                    
                                      point
                                      : 
                                      CodePoint
                                    
                                    
                                      range
                                      : 
                                      CodeRange
                                    
                                  
                                
                                : 
                                boolean
                              
                              

Parameters


isPointInCodeSelection

                            
                              
                                public 
                                isPointInCodeSelection
                                
                                  
                                    
                                      point
                                      : 
                                      CodePoint
                                    
                                    
                                      selection
                                      : 
                                      CodeSelection
                                    
                                  
                                
                                : 
                                boolean
                              
                              

Parameters


lines

                            
                              
                                public 
                                lines
                                
                                  
                                    
                                      from
                                      : 
                                      number
                                    
                                    
                                      to
                                      ?: 
                                      number
                                    
                                  
                                
                                : 
                                CodeRange
                              
                              

Create a code range that spans the given lines.

Parameters

  • from : number

    The line from which the range starts.

  • to ?: number

    The line at which the range ends. If omitted, the range willcover only one line.


metricsToFragment

                            
                              
                                public 
                                metricsToFragment
                                
                                  
                                    
                                      value
                                      : 
                                      CodeMetrics
                                    
                                  
                                
                                : 
                                CodeFragment
                              
                              

Parameters


parseCodeFragment

                            
                              
                                public 
                                parseCodeFragment
                                
                                  
                                    
                                      value
                                      : 
                                      PossibleCodeFragment
                                    
                                    
                                      context
                                      : 
                                      CanvasRenderingContext2D
                                    
                                    
                                      monoWidth
                                      : 
                                      number
                                    
                                  
                                
                                : 
                                CodeFragment
                              
                              

Parameters


parseCodeScope

                            
                              
                                public 
                                parseCodeScope
                                
                                  
                                    
                                      value
                                      : 
                                      PossibleCodeScope
                                    
                                  
                                
                                : 
                                CodeScope
                              
                              

Parameters


parseCodeSelection

                            
                              
                                public 
                                parseCodeSelection
                                
                                  
                                    
                                      value
                                      : 
                                      PossibleCodeSelection
                                    
                                  
                                
                                : 
                                CodeSelection
                              
                              

Parameters


patienceDiff

                            
                              
                                public 
                                patienceDiff
                                
                                  
                                    
                                      aLines
                                      : 
                                      string
                                      []
                                    
                                    
                                      bLines
                                      : 
                                      string
                                      []
                                    
                                  
                                
                                : 
                                
                                  
                                    
                                      lineCountDeleted
                                      : 
                                      number
                                    
                                    
                                      lineCountInserted
                                      : 
                                      number
                                    
                                    
                                      lines
                                      : 
                                      
                                        
                                          
                                            aIndex
                                            : 
                                            number
                                          
                                          
                                            bIndex
                                            : 
                                            number
                                          
                                          
                                            line
                                            : 
                                            string
                                          
                                        
                                      
                                      []
                                    
                                  
                                
                              
                              

Performs a patience diff on two arrays of strings, returning an objectcontaining the lines that were deleted, inserted, and potentially movedlines. The plus parameter can result in a significant performance hit dueto additional Longest Common Substring searches.

Parameters

  • aLines : string []

    The original array of strings

  • bLines : string []

    The new array of strings


pointToPoint

                            
                              
                                public 
                                pointToPoint
                                
                                  
                                    
                                      startLine
                                      : 
                                      number
                                    
                                    
                                      startColumn
                                      : 
                                      number
                                    
                                    
                                      endLine
                                      : 
                                      number
                                    
                                    
                                      endColumn
                                      : 
                                      number
                                    
                                  
                                
                                : 
                                CodeRange
                              
                              

Create a custom selection range.

Parameters

  • startLine : number

    The line at which the selection starts.

  • startColumn : number

    The column at which the selection starts.

  • endLine : number

    The line at which the selection ends.

  • endColumn : number

    The column at which the selection ends.


remove

                            
                              
                                public 
                                remove
                                
                                  
                                    
                                      code
                                      : 
                                      string
                                    
                                  
                                
                                : 
                                RawCodeFragment
                              
                              

Create a code fragment that represents a removal of code.

Can be used in conjunction with edit .

Parameters

  • code : string

    The code to remove.


replace

                            
                              
                                public 
                                replace
                                
                                  
                                    
                                      before
                                      : 
                                      string
                                    
                                    
                                      after
                                      : 
                                      string
                                    
                                  
                                
                                : 
                                RawCodeFragment
                              
                              

Create a code fragment that represents a change from one piece of code toanother.

Can be used in conjunction with edit .

Parameters

  • before : string

    The code to change from.

  • after : string

    The code to change to.


resolveCodeTag

                            
                              
                                public 
                                resolveCodeTag
                                
                                  
                                    
                                      wrapped
                                      : 
                                      CodeTag
                                    
                                    
                                      after
                                      : 
                                      boolean
                                    
                                    
                                      isAfter
                                      : 
                                      IsAfterPredicate
                                       = 
                                      after
                                    
                                  
                                
                                : 
                                string
                              
                              

Parameters


resolveScope

                            
                              
                                public 
                                resolveScope
                                
                                  
                                    
                                      scope
                                      : 
                                      CodeScope
                                    
                                    
                                      isAfter
                                      : 
                                      IsAfterPredicate
                                    
                                  
                                
                                : 
                                string
                              
                              

Parameters


word

                            
                              
                                public 
                                word
                                
                                  
                                    
                                      line
                                      : 
                                      number
                                    
                                    
                                      from
                                      : 
                                      number
                                    
                                    
                                      length
                                      ?: 
                                      number
                                    
                                  
                                
                                : 
                                CodeRange
                              
                              

Create a code range that highlights the given word.

Parameters

  • line : number

    The line at which the word appears.

  • from : number

    The column at which the word starts.

  • length ?: number

    The length of the word. If omitted, the range will cover therest of the line.