Skip to content

Commit 02424ad

Browse files
committed
fix: Ai related bug
1 parent 8eefe20 commit 02424ad

File tree

14 files changed

+700
-251
lines changed

14 files changed

+700
-251
lines changed

dist/main.js

Lines changed: 24 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AcodeX.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,20 +1468,37 @@ export default class AcodeX {
14681468
}
14691469
}
14701470

1471+
async getOllamaModel(aiResponseHandler) {
1472+
const savedLocalLLM = localStorage.getItem("ACODEX_LOCAL_LLM_MODEL");
1473+
if (savedLocalLLM) return savedLocalLLM;
1474+
1475+
try {
1476+
const models = await aiResponseHandler.getListOfOllamaModels();
1477+
if (!Array.isArray(models)) {
1478+
throw new Error("Failed to fetch Ollama models");
1479+
}
1480+
const ollamaModel = await select("Select Local Model", models);
1481+
if (!ollamaModel) return null;
1482+
1483+
localStorage.setItem("ACODEX_LOCAL_LLM_MODEL", ollamaModel);
1484+
return ollamaModel;
1485+
} catch (error) {
1486+
console.error("Error getting Ollama models:", error);
1487+
acode.alert("Error", "Failed to fetch Ollama models");
1488+
return null;
1489+
}
1490+
}
1491+
14711492
async openAIPromptPopup() {
1472-
const aiResponseHandler =
1493+
const aiResponseHandler = new AIResponseHandler(
14731494
this.settings.aiModel === AVAILABLE_AI_MODELS[3][0]
1474-
? new AIResponseHandler()
1475-
: new AIResponseHandler(this.settings.aiApiKey);
1476-
const savedLocalLLM = localStorage.getItem("ACODEX_LOCAL_LLM_MODEL");
1495+
? null
1496+
: this.settings.aiApiKey,
1497+
);
14771498
let ollamaModel;
1478-
if (!savedLocalLLM) {
1479-
const models = await aiResponseHandler.getListOfOllamaModels();
1480-
ollamaModel = await select("Select Local Model", models);
1499+
if (this.settings.aiModel === "local-llm") {
1500+
ollamaModel = await this.getOllamaModel(aiResponseHandler);
14811501
if (!ollamaModel) return;
1482-
localStorage.setItem("ACODEX_LOCAL_LLM_MODEL", ollamaModel);
1483-
} else {
1484-
ollamaModel = savedLocalLLM;
14851502
}
14861503
const promptBox = DialogBox(
14871504
"⚡ Ask AcodeX Ai",

src/addons/ligatures.js

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,73 @@
11
export default class LigaturesAddon {
22
constructor(options = {}) {
33
// fallback ligatures if a font does not support ligatures natively
4-
this._fallbackLigatures = options.fallbackLigatures || [
5-
'<--', '<---', '<<-', '<-', '->', '->>', '-->', '--->',
6-
'<==', '<===', '<<=', '<=', '=>', '=>>', '==>', '===>', '>=', '>>=',
7-
'<->', '<-->', '<--->', '<---->', '<=>', '<==>', '<===>', '<====>',
8-
'<~~', '<~', '~>', '~~>', '::', ':::', '==', '!=', '===', '!==', ':=',
9-
':-', ':+', '<*', '<*>', '*>', '<|', '<|>', '|>', '+:', '-:', '=:', ':>',
10-
'++', '+++', '<!--', '<!---', '<***>'
11-
].sort((a, b) => b.length - a.length);
4+
this._fallbackLigatures =
5+
options.fallbackLigatures ||
6+
[
7+
"<--",
8+
"<---",
9+
"<<-",
10+
"<-",
11+
"->",
12+
"->>",
13+
"-->",
14+
"--->",
15+
"<==",
16+
"<===",
17+
"<<=",
18+
"<=",
19+
"=>",
20+
"=>>",
21+
"==>",
22+
"===>",
23+
">=",
24+
">>=",
25+
"<->",
26+
"<-->",
27+
"<--->",
28+
"<---->",
29+
"<=>",
30+
"<==>",
31+
"<===>",
32+
"<====>",
33+
"<~~",
34+
"<~",
35+
"~>",
36+
"~~>",
37+
"::",
38+
":::",
39+
"==",
40+
"!=",
41+
"===",
42+
"!==",
43+
":=",
44+
":-",
45+
":+",
46+
"<*",
47+
"<*>",
48+
"*>",
49+
"<|",
50+
"<|>",
51+
"|>",
52+
"+:",
53+
"-:",
54+
"=:",
55+
":>",
56+
"++",
57+
"+++",
58+
"<!--",
59+
"<!---",
60+
"<***>",
61+
].sort((a, b) => b.length - a.length);
1262
this._characterJoinerId = undefined;
1363
this._terminal = undefined;
1464
}
1565

1666
activate(terminal) {
1767
this._terminal = terminal;
18-
this._characterJoinerId = terminal.registerCharacterJoiner(this._joinCharacters.bind(this));
68+
this._characterJoinerId = terminal.registerCharacterJoiner(
69+
this._joinCharacters.bind(this),
70+
);
1971
terminal.element.style.fontFeatureSettings = `"liga" on, "calt" on`;
2072
}
2173

@@ -25,7 +77,7 @@ export default class LigaturesAddon {
2577
this._characterJoinerId = undefined;
2678
}
2779
if (this._terminal?.element) {
28-
this._terminal.element.style.fontFeatureSettings = '';
80+
this._terminal.element.style.fontFeatureSettings = "";
2981
}
3082
}
3183

@@ -36,7 +88,7 @@ export default class LigaturesAddon {
3688
_findLigatureRanges(text, ligatures) {
3789
const ranges = [];
3890
for (let i = 0; i < text.length; i++) {
39-
for (let ligature of ligatures) {
91+
for (const ligature of ligatures) {
4092
if (text.startsWith(ligature, i)) {
4193
ranges.push([i, i + ligature.length]);
4294
i += ligature.length - 1;
@@ -46,4 +98,4 @@ export default class LigaturesAddon {
4698
}
4799
return ranges;
48100
}
49-
}
101+
}

src/core/selectionCore.js

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ export default class SelectionCore {
99
touchStartY = 0;
1010
touchStartTime = 0;
1111
scrollThreshold = 10; // pixels
12-
scrollTimeThreshold = 100; // milliseconds
13-
14-
constructor(terminal, startHandle, endHandle, terminalContainer, terminalHeader) {
12+
scrollTimeThreshold = 100; // milliseconds
13+
14+
constructor(
15+
terminal,
16+
startHandle,
17+
endHandle,
18+
terminalContainer,
19+
terminalHeader,
20+
) {
1521
this.terminal = terminal;
1622
this.startHandle = startHandle;
1723
this.endHandle = endHandle;
@@ -23,7 +29,7 @@ export default class SelectionCore {
2329
const renderer = this.terminal._core._renderService.dimensions;
2430
return {
2531
cellWidth: renderer.css.cell.width,
26-
cellHeight: renderer.css.cell.height
32+
cellHeight: renderer.css.cell.height,
2733
};
2834
}
2935

@@ -74,18 +80,20 @@ export default class SelectionCore {
7480
handle.style.top = `${y}px`;
7581
handle.style.display = 'block';
7682
}*/
77-
83+
7884
setHandlePosition(handle, row, column, isStartHandle = false) {
7985
const { cellWidth, cellHeight } = this._getCellSize();
8086
const rect = this.terminal.element.getBoundingClientRect();
8187

8288
const terminalContainer = this.terminalContainer;
8389
const terminalHeader = this.terminalHeader;
84-
90+
8591
// Heights
8692
const terminalContainerRect = terminalContainer.getBoundingClientRect();
87-
const terminalHeaderHeight = terminalHeader ? terminalHeader.getBoundingClientRect().height : 0;
88-
93+
const terminalHeaderHeight = terminalHeader
94+
? terminalHeader.getBoundingClientRect().height
95+
: 0;
96+
8997
// Terminal scroll position and viewport Y-offset
9098
const terminalScrollOffset = this.terminal.element.scrollTop || 0;
9199
const viewportScrollOffset = this.terminal.buffer.active.viewportY;
@@ -95,13 +103,20 @@ export default class SelectionCore {
95103

96104
// X position based on column
97105
let x = isStartHandle
98-
? rect.left + column * cellWidth - 10
99-
: rect.left + (column + 1) * cellWidth - cellWidth;
106+
? rect.left + column * cellWidth - 10
107+
: rect.left + (column + 1) * cellWidth - cellWidth;
100108

101-
let y = rect.top + (adjustedRow * cellHeight) - terminalHeaderHeight - terminalScrollOffset - cellHeight;
109+
let y =
110+
rect.top +
111+
adjustedRow * cellHeight -
112+
terminalHeaderHeight -
113+
terminalScrollOffset -
114+
cellHeight;
102115

103-
const isSwapped = this.selectionStart.row > this.selectionEnd.row ||
104-
(this.selectionStart.row === this.selectionEnd.row && this.selectionStart.column > this.selectionEnd.column);
116+
const isSwapped =
117+
this.selectionStart.row > this.selectionEnd.row ||
118+
(this.selectionStart.row === this.selectionEnd.row &&
119+
this.selectionStart.column > this.selectionEnd.column);
105120

106121
if (isSwapped) {
107122
x = !isStartHandle
@@ -111,22 +126,25 @@ export default class SelectionCore {
111126

112127
// Ensure the handle stays within bounds of terminal
113128
x = Math.max(rect.left, Math.min(x, rect.right - handle.offsetWidth));
114-
y = Math.max(terminalContainerRect.top, Math.min(y, terminalContainerRect.bottom - handle.offsetHeight));
129+
y = Math.max(
130+
terminalContainerRect.top,
131+
Math.min(y, terminalContainerRect.bottom - handle.offsetHeight),
132+
);
115133

116134
// Set the position of the handle
117135
handle.style.left = `${x}px`;
118136
handle.style.top = `${y}px`;
119-
handle.style.display = 'block';
137+
handle.style.display = "block";
120138
}
121139

122140
hideHandles() {
123-
this.startHandle.style.display = 'none';
124-
this.endHandle.style.display = 'none';
141+
this.startHandle.style.display = "none";
142+
this.endHandle.style.display = "none";
125143
}
126144

127145
showHandles() {
128-
this.startHandle.style.display = 'block';
129-
this.endHandle.style.display = 'block';
146+
this.startHandle.style.display = "block";
147+
this.endHandle.style.display = "block";
130148
}
131149

132150
startSelection(row, column) {
@@ -150,29 +168,47 @@ export default class SelectionCore {
150168

151169
// start is always before end in the terminal's text flow
152170
if (startRow > endRow || (startRow === endRow && startColumn > endColumn)) {
153-
[startRow, startColumn, endRow, endColumn] = [endRow, endColumn, startRow, startColumn];
171+
[startRow, startColumn, endRow, endColumn] = [
172+
endRow,
173+
endColumn,
174+
startRow,
175+
startColumn,
176+
];
154177
}
155178

156-
const totalLength = this._calculateTotalSelectionLength(startRow, endRow, startColumn, endColumn);
179+
const totalLength = this._calculateTotalSelectionLength(
180+
startRow,
181+
endRow,
182+
startColumn,
183+
endColumn,
184+
);
157185
this.terminal.select(startColumn, startRow, totalLength);
158186

159187
// Set handle positions based on their actual positions, not the selection bounds
160-
this.setHandlePosition(this.startHandle, this.selectionStart.row, this.selectionStart.column, true);
161-
this.setHandlePosition(this.endHandle, this.selectionEnd.row, this.selectionEnd.column);
188+
this.setHandlePosition(
189+
this.startHandle,
190+
this.selectionStart.row,
191+
this.selectionStart.column,
192+
true,
193+
);
194+
this.setHandlePosition(
195+
this.endHandle,
196+
this.selectionEnd.row,
197+
this.selectionEnd.column,
198+
);
162199
}
163200

164201
_calculateTotalSelectionLength(startRow, endRow, startColumn, endColumn) {
165202
const terminalCols = this.terminal.cols;
166203

167204
if (startRow === endRow) {
168205
return Math.abs(endColumn - startColumn) + 1;
169-
} else {
170-
let length = 0;
171-
length += terminalCols - startColumn;
172-
length += (endRow - startRow - 1) * terminalCols;
173-
length += endColumn + 1;
174-
return length;
175206
}
207+
let length = 0;
208+
length += terminalCols - startColumn;
209+
length += (endRow - startRow - 1) * terminalCols;
210+
length += endColumn + 1;
211+
return length;
176212
}
177213

178214
startHandleTouchMoveCb(event) {
@@ -204,7 +240,7 @@ export default class SelectionCore {
204240

205241
this.tapHoldTimeout = setTimeout(() => {
206242
this.isTapAndHoldActive = true;
207-
this.terminal.focus()
243+
this.terminal.focus();
208244
this.startSelection(coords.row, coords.column);
209245
}, 500);
210246
}
@@ -223,7 +259,10 @@ export default class SelectionCore {
223259
const touchMoveDelta = Math.abs(this.touchMoveY - this.touchStartY);
224260
const touchMoveTime = Date.now() - this.touchStartTime;
225261

226-
if (touchMoveDelta > this.scrollThreshold && touchMoveTime < this.scrollTimeThreshold) {
262+
if (
263+
touchMoveDelta > this.scrollThreshold &&
264+
touchMoveTime < this.scrollTimeThreshold
265+
) {
227266
clearTimeout(this.tapHoldTimeout);
228267
}
229268
}
@@ -251,4 +290,4 @@ export default class SelectionCore {
251290
this.hideHandles();
252291
}
253292
}
254-
}
293+
}

src/main.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ if (window.acode) {
55
const acodePlugin = new AcodeX();
66
acode.setPluginInit(
77
plugin.id,
8-
async (baseUrl, $page, { cacheFileUrl, cacheFile }) => {
9-
if (!baseUrl.endsWith("/")) {
10-
baseUrl += "/";
11-
}
8+
async (initUrl, $page, { cacheFileUrl, cacheFile }) => {
9+
const baseUrl = initUrl.endsWith("/") ? initUrl : `${initUrl}/`;
1210
acodePlugin.baseUrl = baseUrl;
1311
await acodePlugin.init($page, cacheFile, cacheFileUrl);
1412
},
15-
acodePlugin.settingsObj
13+
acodePlugin.settingsObj,
1614
);
1715
acode.setPluginUnmount(plugin.id, () => {
1816
acodePlugin.destroy();

0 commit comments

Comments
 (0)