Skip to content

Commit

Permalink
Respect files.encoding setting in ripgrep search - #19983
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Mar 17, 2017
1 parent fa50b52 commit 8a6057a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/vs/base/node/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,39 @@ export function detectEncodingByBOMFromBuffer(buffer: NodeBuffer, bytesRead: num
*/
export function detectEncodingByBOM(file: string): TPromise<string> {
return stream.readExactlyByFile(file, 3).then(({buffer, bytesRead}) => detectEncodingByBOMFromBuffer(buffer, bytesRead));
}

/**
* The encodings that are allowed in a settings file don't match the canonical encoding labels specified by WHATWG.
* See https://encoding.spec.whatwg.org/#names-and-labels
* Iconv-lite strips all non-alphanumeric characters, but ripgrep doesn't. For backcompat, allow these labels.
*/
export function toCanonicalName(enc: string): string {
switch (enc) {
case 'shiftjis':
return 'shift-jis';
case 'utf16le':
return 'utf-16le';
case 'utf16be':
return 'utf-16be';
case 'big5hkcs':
return 'big5-hkcs';
case 'eucjp':
return 'euc-jp';
case 'euckr':
return 'euc-kr';
case 'koi8r':
return 'koi8-r';
case 'koi8u':
return 'koi8-u';
case 'macroman':
return 'x-mac-roman';
default:
const m = enc.match(/windows(\d+)/);
if (m) {
return 'windows-' + m[1];
}

return enc;
}
}
6 changes: 6 additions & 0 deletions src/vs/workbench/services/search/node/ripgrepTextSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as path from 'path';
import * as cp from 'child_process';
import { rgPath } from 'vscode-ripgrep';

import * as encoding from 'vs/base/node/encoding';
import * as strings from 'vs/base/common/strings';
import * as glob from 'vs/base/common/glob';
import { ILineMatch, IProgress } from 'vs/platform/search/common/search';
Expand Down Expand Up @@ -325,6 +326,11 @@ function getRgArgs(config: IRawSearch): { args: string[], siblingClauses: glob.S
// Follow symlinks
args.push('--follow');

// Set default encoding
if (config.fileEncoding) {
args.push('--encoding', encoding.toCanonicalName(config.fileEncoding));
}

if (config.contentPattern.isRegExp) {
if (config.contentPattern.isWordMatch) {
args.push('--word-regexp');
Expand Down

0 comments on commit 8a6057a

Please sign in to comment.