PageSourceSearch

https://hamishtodd1.github.io/ga/ed/mention.js

js hamishtodd1.github.io collected 2026-10-03 09:17:00 UTC 5,354 bytes, 162 lines download raw bytes

1//plan for arrays:
2//you have to extract them one at a time, because what if they're 4x4 matrices?
3
4function initMentions() {
5    
6    let style = window.getComputedStyle(textarea)
7    let textareaOffsetHorizontal = parseInt(style.padding) + parseInt(style.margin) // can add some fudge to this if you like
8    let textareaOffsetVertical = parseInt(style.top) + parseInt(style.padding) + parseInt(style.margin)
9    let characterWidth = parseInt(window.getComputedStyle(textMeasurer).width) / 40.
10
11    forEachUsedMention = (func) => {
12        variables.forEach((v) => {
13            for (let i = 0; i < v.lowestUnusedMention; ++i)
14                func(v.mentions[i])
15        })
16    }
17
18    lineToScreenY = (line) => {
19        return line * lineHeight + textareaOffsetVertical - textarea.scrollTop
20    }
21    columnToScreenX = (column) => {
22        return column * characterWidth + textareaOffsetHorizontal - textarea.scrollLeft
23    }
24    
25    hideHighlight = () => {
26        hideLabelLines()
27        forEachPropt(dws, (dw) => {
28            dw.setBorderHighlight(false)
29        })
30    }
31
32    getIndicatedTextareaMention = (screenX, screenY) => {
33        let ret = null
34        forEachUsedMention((mention)=>{
35            if (inamongstChangedLines(mention.lineIndex) )
36                return false
37
38            let y = lineToScreenY(mention.lineIndex)
39            let x = columnToScreenX(mention.column)
40            let w = mention.charactersWide * characterWidth
41
42            let xyInBox =
43                x <= screenX && screenX <= x + w &&
44                y <= screenY && screenY < y + lineHeight
45
46            if(xyInBox)
47                ret = mention
48        })
49
50        return ret
51    }
52
53    class Mention {
54        //these three are all you need for equationarea mentions
55        //indexInArray is -1, so assignmentToOutput is from the variable
56        variable
57        appearance = null
58        mentionIndex = -1
59
60        lineIndex = -1
61        column = -1
62        charactersWide
63
64        //if you have arr[0], then later, arr[1], these are different appearances 
65        //and so should not affect each other. Yes they have the same variable, no they should not be compared
66        indexInArray = -1
67        assignmentToOutput = ""
68
69        constructor(variable) {
70            this.variable = variable
71        }
72
73        setIndexInArray(indexInArray) {
74            if (indexInArray !== this.indexInArray && indexInArray !== -1) {
75                for (let i = 0; i < this.variable.type.numFloats; ++i)
76                    this.assignmentToOutput += 
77                        `    outputFloats[` + i + `] = ` + 
78                        this.variable.name + `[` + indexInArray + `]` + 
79                        this.variable.type.outputAssignmentPropts[i] + `;\n`
80            }
81
82            this.indexInArray = indexInArray
83        }
84
85        getFullName() {
86            return this.variable.name + (this.indexInArray === -1 ? `` : `[` + this.indexInArray + `]` )
87        }
88
89        getAssignmentToOutput() {
90            if( this.indexInArray === -1)
91                return this.variable.assignmentToOutput
92            else
93                return this.assignmentToOutput
94        }
95
96        highlight() {
97            //mouse box
98            let y = lineToScreenY(this.lineIndex)
99            let x = columnToScreenX(this.column)
100            let w = this.charactersWide * characterWidth
101
102            highlightAppearance(this.appearance, this.variable.col, x,y,w)
103        }
104    }
105    window.Mention = Mention
106
107    //a mention can be either an array element or an array
108    //a variable is just a name really
109    //would you like both to have the same variable?
110    //  Well, should have same color, isIn and isUniform. name is dubious
111
112    let randomColor = new THREE.Color()
113    let currentHue = 0.
114    let goldenRatio = (Math.sqrt(5.) + 1.) / 2.
115    class Variable {
116        name
117        type
118        col = new THREE.Color(0., 0., 0.)
119
120        //could have something to indicate it's neither of these. A "variable" I guess
121        isIn = false
122        isUniform = false
123
124        lowestUnusedMention = 0 //well, index thereof. Maybe change
125        mentions = []
126
127        assignmentToOutput = ``
128        
129        arrayLength = -1
130        
131        constructor(newName, newClass, newArrayLength) {
132            //never changed after this
133            this.name = newName
134            this.type = newClass
135            this.arrayLength = newArrayLength
136
137            for (let i = 0; i < this.type.numFloats; ++i)
138                this.assignmentToOutput += `    outputFloats[` + i + `] = ` + this.name + this.type.outputAssignmentPropts[i] + `;
138\n`
139
140            randomColor.setHSL(currentHue, 1., .5)
141            currentHue += 1. / goldenRatio
142            while (currentHue > 1.)
143                currentHue -= 1.
144            this.col.r = randomColor.r; this.col.g = randomColor.g; this.col.b = randomColor.b;
145
146            variables.push(this)
147        }
148
149        isArray() {
150            return this.arrayLength !== -1
151        }
152
153        getLowestUnusedMention() {
154            console.assert(this.mentions.length >= this.lowestUnusedMention)
155            if (this.mentions.length === this.lowestUnusedMention)
156                this.mentions.push(new Mention(this))
157
158            return this.mentions[this.lowestUnusedMention++]
159        }
160    }
161    window.Variable = Variable
162}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.