Skip to content

Commit d4ca906

Browse files
STetsingAniket-Engg
authored andcommitted
considering user viewed file range as context
1 parent edf835e commit d4ca906

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed

libs/remix-ui/editor/src/lib/providers/inlineCompletionProvider.ts

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,67 @@ export class RemixInLineCompletionProvider implements monacoTypes.languages.Inli
8787
return model.getValueInRange(lineRange);
8888
}
8989

90-
// Get text before and after cursor
91-
const word = model.getValueInRange({
92-
startLineNumber: 1,
93-
startColumn: 1,
94-
endLineNumber: position.lineNumber,
95-
endColumn: position.column,
96-
});
90+
// Get viewport-aware context (what user actually sees on screen)
91+
const getViewportContext = (model: monacoTypes.editor.ITextModel, position: monacoTypes.Position, editor?: monacoTypes.editor.ICodeEditor) => {
92+
let visibleRange = null;
93+
94+
// Try to get the visible range from the editor if available
95+
if (editor && editor.getVisibleRanges) {
96+
const visibleRanges = editor.getVisibleRanges();
97+
if (visibleRanges && visibleRanges.length > 0) {
98+
visibleRange = visibleRanges[0];
99+
}
100+
}
97101

98-
const word_after = model.getValueInRange({
99-
startLineNumber: position.lineNumber,
100-
startColumn: position.column,
101-
endLineNumber: model.getLineCount(),
102-
endColumn: getTextAtLine(model.getLineCount()).length + 1,
103-
});
102+
// Fallback: approximate visible range (about 30 lines above/below cursor)
103+
if (!visibleRange) {
104+
const approximateViewportSize = 30;
105+
const startLine = Math.max(1, position.lineNumber - approximateViewportSize);
106+
const endLine = Math.min(model.getLineCount(), position.lineNumber + approximateViewportSize);
107+
108+
visibleRange = {
109+
startLineNumber: startLine,
110+
startColumn: 1,
111+
endLineNumber: endLine,
112+
endColumn: model.getLineMaxColumn(endLine)
113+
};
114+
}
115+
116+
const contextBefore = model.getValueInRange({
117+
startLineNumber: Math.max(visibleRange.startLineNumber, 1),
118+
startColumn: visibleRange.startLineNumber === position.lineNumber ? 1 : 1,
119+
endLineNumber: position.lineNumber,
120+
endColumn: position.column,
121+
});
122+
123+
const contextAfter = model.getValueInRange({
124+
startLineNumber: position.lineNumber,
125+
startColumn: position.column,
126+
endLineNumber: Math.min(visibleRange.endLineNumber, model.getLineCount()),
127+
endColumn: visibleRange.endLineNumber === model.getLineCount()
128+
? getTextAtLine(model.getLineCount()).length + 1
129+
: model.getLineMaxColumn(visibleRange.endLineNumber),
130+
});
131+
132+
return { contextBefore, contextAfter };
133+
};
134+
135+
// Try to get the editor instance for accurate viewport detection
136+
let editor = null;
137+
try {
138+
// Access the editor through Monaco's editor instances
139+
const editorInstances = this.monaco.editor.getEditors();
140+
if (editorInstances && editorInstances.length > 0) {
141+
// splitted editors not handled now
142+
editor = editorInstances.find(e => e.getModel() === model) || editorInstances[0];
143+
}
144+
} catch (e) {
145+
console.debug('Could not access editor instance for viewport detection:', e);
146+
}
147+
148+
const { contextBefore, contextAfter } = getViewportContext(model, position, editor);
149+
const word = contextBefore;
150+
const word_after = contextAfter;
104151

105152
// Create cache key and check cache
106153
const cacheKey = this.cache.createCacheKey(word, word_after, position, this.task);

0 commit comments

Comments
 (0)