Skip to content

Commit 02d04b4

Browse files
committed
Prevent setting ranges on binary files
- should bot be possible to set ranges on binary files [Accuracy boost] └── apps └── editor ├── package.json └── src ├── commands │ ├── index.ts │ └── set-ranges-command.ts ├── constants │ └── state-keys.ts ├── context │ └── providers │ ├── open-editors │ │ └── open-editors-provider.ts │ └── workspace │ └── workspace-provider.ts ├── extension.ts ├── features │ └── search-files │ └── utils │ └── analyze-files.ts ├── i18n │ └── translations │ ├── commands │ │ └── set-ranges-command.ts │ └── services │ └── websocket-manager.ts ├── utils │ └── prompt-builder.ts └── views └── prompt └── backend └── message-handlers └── utils └── build-prompt-payload.ts
1 parent 6791c13 commit 02d04b4

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎apps/editor/src/commands/set-ranges-command.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { t } from '../i18n'
1313
import { normalize_path } from '../utils/normalize-path'
1414
import { get_error_message } from '@/utils/get-error-message'
15+
import { is_binary_file } from '../utils/is-binary'
1516

1617
export const set_ranges_command = (
1718
workspace_provider: WorkspaceProvider,
@@ -37,6 +38,24 @@ export const set_ranges_command = (
3738
return
3839
}
3940

41+
let is_binary = is_binary_file(file_path)
42+
if (!is_binary) {
43+
try {
44+
const file_handle = await fs.open(file_path, 'r')
45+
const buffer = Buffer.alloc(8000)
46+
const { bytesRead } = await file_handle.read(buffer, 0, 8000, 0)
47+
await file_handle.close()
48+
is_binary = is_binary_file(file_path, buffer.subarray(0, bytesRead))
49+
} catch {}
50+
}
51+
52+
if (is_binary) {
53+
vscode.window.showErrorMessage(
54+
t('command.set-ranges-command.is-binary')
55+
)
56+
return
57+
}
58+
4059
const relative_path = path.relative(
4160
workspace_folder.uri.fsPath,
4261
file_path

‎apps/editor/src/i18n/translations/commands/set-ranges-command.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
export const translations = {
2+
'command.set-ranges-command.is-binary': {
3+
en: 'Cannot set ranges on binary files.',
4+
pl: 'Nie można ustawiać zakresów w plikach binarnych.',
5+
'zh-cn': '不能在二进制文件上设置范围。',
6+
ja: 'バイナリファイルに範囲を設定することはできません。',
7+
'zh-tw': '不能在二進位檔案上設定範圍。',
8+
de: 'Für Binärdateien können keine Bereiche festgelegt werden.',
9+
es: 'No se pueden establecer rangos en archivos binarios.',
10+
fr: 'Impossible de définir des plages sur des fichiers binaires.',
11+
'pt-br': 'Não é possível definir intervalos em arquivos binários.',
12+
ru: 'Невозможно установить диапазоны для бинарных файлов.',
13+
ko: '이진 파일에는 범위를 설정할 수 없습니다.',
14+
it: 'Impossibile impostare intervalli sui file binari.',
15+
tr: 'İkili dosyalarda aralıklar ayarlanamaz.',
16+
cs: 'Nelze nastavit rozsahy v binárních souborech.',
17+
hu: 'Bináris fájlok esetén nem lehet tartományokat beállítani.',
18+
bg: 'Не могат да се задават диапазони в двоични файлове.'
19+
},
220
'command.set-ranges-command.not-in-workspace': {
321
en: 'File is not in a workspace folder.',
422
pl: 'Plik nie znajduje się w folderze obszaru roboczego.',

0 commit comments

Comments
 (0)