Skip to content

Commit f6825ae

Browse files
authored
Merge pull request #30 from Daydreamer-riri/fix/eol
fix: auto detect EOL
2 parents e828a99 + 365a83e commit f6825ae

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/CompletionProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {EOL} from 'os';
21
import {CompletionItem, type Position} from 'vscode-languageserver-protocol';
32
import type {TextDocument} from 'vscode-languageserver-textdocument';
43
import * as lsp from 'vscode-languageserver/node';
@@ -7,6 +6,7 @@ import {
76
findImportPath,
87
getAllClassNames,
98
getCurrentDirFromUri,
9+
getEOL,
1010
getTransformer,
1111
} from './utils';
1212
import type {CamelCaseValues} from './utils';
@@ -170,7 +170,7 @@ export class CSSModulesCompletionProvider {
170170
position: Position,
171171
): Promise<CompletionItem[] | null> {
172172
const fileContent = textdocument.getText();
173-
const lines = fileContent.split(EOL);
173+
const lines = fileContent.split(getEOL(fileContent));
174174
const currentLine = lines[position.line];
175175
if (typeof currentLine !== 'string') return null;
176176
const currentDir = getCurrentDirFromUri(textdocument.uri);

src/DefinitionProvider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {EOL} from 'os';
21
import path from 'path';
32
import {
43
type Hover,
@@ -16,6 +15,7 @@ import {
1615
findImportPath,
1716
genImportRegExp,
1817
getCurrentDirFromUri,
18+
getEOL,
1919
getPosition,
2020
getTransformer,
2121
getWords,
@@ -57,6 +57,7 @@ export class CSSModulesDefinitionProvider {
5757
position: Position,
5858
): Promise<null | Hover> {
5959
const fileContent = textdocument.getText();
60+
const EOL = getEOL(fileContent);
6061
const lines = fileContent.split(EOL);
6162
const currentLine = lines[position.line];
6263

@@ -93,6 +94,7 @@ export class CSSModulesDefinitionProvider {
9394
field,
9495
node.declarations,
9596
node.comments,
97+
EOL,
9698
),
9799
},
98100
};
@@ -103,7 +105,7 @@ export class CSSModulesDefinitionProvider {
103105
position: Position,
104106
): Promise<Location | null> {
105107
const fileContent = textdocument.getText();
106-
const lines = fileContent.split(EOL);
108+
const lines = fileContent.split(getEOL(fileContent));
107109
const currentLine = lines[position.line];
108110

109111
if (typeof currentLine !== 'string') {

src/utils.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fs from 'fs';
2-
import {EOL} from 'os';
32
import path from 'path';
43
import url from 'url';
54
import _camelCase from 'lodash.camelcase';
@@ -266,6 +265,7 @@ export async function filePathToClassnameDict(
266265
classnameTransformer: StringTransformer,
267266
): Promise<ClassnameDict> {
268267
const content = fs.readFileSync(filepath, {encoding: 'utf8'});
268+
const EOL = getEOL(content);
269269
const {ext} = path.parse(filepath);
270270

271271
/**
@@ -427,6 +427,7 @@ export function stringifyClassname(
427427
classname: string,
428428
declarations: string[],
429429
comments: string[],
430+
EOL: string,
430431
): string {
431432
const commentString = comments.length
432433
? comments
@@ -452,3 +453,20 @@ export function stringifyClassname(
452453
].join(EOL)
453454
);
454455
}
456+
457+
// https://github.com/wkillerud/some-sass/blob/main/vscode-extension/src/utils/string.ts
458+
export function getEOL(text: string): string {
459+
for (let i = 0; i < text.length; i++) {
460+
const ch = text.charAt(i);
461+
if (ch === '\r') {
462+
if (i + 1 < text.length && text.charAt(i + 1) === '\n') {
463+
return '\r\n';
464+
}
465+
return '\r';
466+
}
467+
if (ch === '\n') {
468+
return '\n';
469+
}
470+
}
471+
return '\n';
472+
}

0 commit comments

Comments
 (0)