Skip to content

Commit

Permalink
chore: refactor vf LS to use arrow functions
Browse files Browse the repository at this point in the history
@W-14564471@
refactor package VF language server to use arrow functions where applicable
  • Loading branch information
peternhale committed Dec 28, 2023
1 parent 369750b commit c2680df
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 183 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rules": {
"prefer-arrow/prefer-arrow-functions": ["error", {}],
"@typescript-eslint/no-unsafe-assignment": "warn",
"@typescript-eslint/no-misused-promises": "warn",
"@typescript-eslint/no-unsafe-argument": "warn",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export interface LanguageModelCache<T> {
dispose(): void;
}

export function getLanguageModelCache<T>(
export const getLanguageModelCache = <T>(
maxEntries: number,
cleanupIntervalTimeInSec: number,
parse: (document: TextDocument) => T
): LanguageModelCache<T> {
): LanguageModelCache<T> => {
let languageModels: {
[uri: string]: {
version: number;
Expand All @@ -44,7 +44,7 @@ export function getLanguageModelCache<T>(
}

return {
get(document: TextDocument): T {
get: (document: TextDocument): T => {
const version = document.version;
const languageId = document.languageId;
const languageModelInfo = languageModels[document.uri];
Expand Down Expand Up @@ -85,14 +85,14 @@ export function getLanguageModelCache<T>(
}
return languageModel;
},
onDocumentRemoved(document: TextDocument) {
onDocumentRemoved: (document: TextDocument) => {
const uri = document.uri;
if (languageModels[uri]) {
delete languageModels[uri];
nModels--;
}
},
dispose() {
dispose: () => {
if (typeof cleanupInterval !== 'undefined') {
clearInterval(cleanupInterval);
cleanupInterval = void 0;
Expand All @@ -101,4 +101,4 @@ export function getLanguageModelCache<T>(
}
}
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
import { CSS_STYLE_RULE, HTMLDocumentRegions } from './embeddedSupport';
import { ColorInformation, LanguageMode, Settings } from './languageModes';

export function getCSSMode(
export const getCSSMode = (
documentRegions: LanguageModelCache<HTMLDocumentRegions>
): LanguageMode {
): LanguageMode => {
const cssLanguageService = getCSSLanguageService();
const embeddedCSSDocuments = getLanguageModelCache<TextDocument>(
10,
Expand All @@ -28,88 +28,88 @@ export function getCSSMode(
);

return {
getId() {
getId: () => {
return 'css';
},
configure(options: any) {
configure: (options: any) => {
cssLanguageService.configure(options && options.css);
},
doValidation(document: TextDocument, settings: Settings) {
doValidation: (document: TextDocument, settings: Settings) => {
const embedded = embeddedCSSDocuments.get(document);
return cssLanguageService.doValidation(
embedded,
cssStylesheets.get(embedded),
settings && settings.css
);
},
doComplete(document: TextDocument, position: Position) {
doComplete: (document: TextDocument, position: Position) => {
const embedded = embeddedCSSDocuments.get(document);
return cssLanguageService.doComplete(
embedded,
position,
cssStylesheets.get(embedded)
);
},
doHover(document: TextDocument, position: Position) {
doHover: (document: TextDocument, position: Position) => {
const embedded = embeddedCSSDocuments.get(document);
return cssLanguageService.doHover(
embedded,
position,
cssStylesheets.get(embedded)
);
},
findDocumentHighlight(document: TextDocument, position: Position) {
findDocumentHighlight: (document: TextDocument, position: Position) => {
const embedded = embeddedCSSDocuments.get(document);
return cssLanguageService.findDocumentHighlights(
embedded,
position,
cssStylesheets.get(embedded)
);
},
findDocumentSymbols(document: TextDocument) {
findDocumentSymbols: (document: TextDocument) => {
const embedded = embeddedCSSDocuments.get(document);
return cssLanguageService
.findDocumentSymbols(embedded, cssStylesheets.get(embedded))
.filter(s => s.name !== CSS_STYLE_RULE);
},
findDefinition(document: TextDocument, position: Position) {
findDefinition: (document: TextDocument, position: Position) => {
const embedded = embeddedCSSDocuments.get(document);
return cssLanguageService.findDefinition(
embedded,
position,
cssStylesheets.get(embedded)
);
},
findReferences(document: TextDocument, position: Position) {
findReferences: (document: TextDocument, position: Position) => {
const embedded = embeddedCSSDocuments.get(document);
return cssLanguageService.findReferences(
embedded,
position,
cssStylesheets.get(embedded)
);
},
findDocumentColors(document: TextDocument) {
findDocumentColors: (document: TextDocument) => {
const embedded = embeddedCSSDocuments.get(document);
return cssLanguageService.findDocumentColors(
embedded,
cssStylesheets.get(embedded)
);
},
getColorPresentations(document: TextDocument, colorInfo: ColorInformation) {
getColorPresentations: (document: TextDocument, colorInfo: ColorInformation) => {
const embedded = embeddedCSSDocuments.get(document);
return cssLanguageService.getColorPresentations(
embedded,
cssStylesheets.get(embedded),
colorInfo
);
},
onDocumentRemoved(document: TextDocument) {
onDocumentRemoved: (document: TextDocument) => {
embeddedCSSDocuments.onDocumentRemoved(document);
cssStylesheets.onDocumentRemoved(document);
},
dispose() {
dispose: () => {
embeddedCSSDocuments.dispose();
cssStylesheets.dispose();
}
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ interface EmbeddedRegion {
attributeValue?: boolean;
}

export function getDocumentRegions(
export const getDocumentRegions = (
languageService: LanguageService,
document: TextDocument
): HTMLDocumentRegions {
): HTMLDocumentRegions => {
const regions: EmbeddedRegion[] = [];
const scanner = languageService.createScanner(document.getText());
let lastTagName: string;
Expand Down Expand Up @@ -132,13 +132,12 @@ export function getDocumentRegions(
getLanguagesInDocument: () => getLanguagesInDocument(document, regions),
getImportedScripts: () => importedScripts
};
}

function getLanguageRanges(
};
const getLanguageRanges = (
document: TextDocument,
regions: EmbeddedRegion[],
range: Range
): LanguageRange[] {
): LanguageRange[] => {
const result: LanguageRange[] = [];
let currentPos = range ? range.start : Position.create(0, 0);
let currentOffset = range ? document.offsetAt(range.start) : 0;
Expand Down Expand Up @@ -179,12 +178,12 @@ function getLanguageRanges(
});
}
return result;
}
};

function getLanguagesInDocument(
const getLanguagesInDocument = (
document: TextDocument,
regions: EmbeddedRegion[]
): string[] {
): string[] => {
const result = [];
for (const region of regions) {
if (region.languageId && result.indexOf(region.languageId) === -1) {
Expand All @@ -196,13 +195,13 @@ function getLanguagesInDocument(
}
result.push('html');
return result;
}
};

function getLanguageAtPosition(
const getLanguageAtPosition = (
document: TextDocument,
regions: EmbeddedRegion[],
position: Position
): string {
): string => {
const offset = document.offsetAt(position);
for (const region of regions) {
if (region.start <= offset) {
Expand All @@ -214,14 +213,14 @@ function getLanguageAtPosition(
}
}
return 'html';
}
};

function getEmbeddedDocument(
const getEmbeddedDocument = (
document: TextDocument,
contents: EmbeddedRegion[],
languageId: string,
ignoreAttributeValues: boolean
): TextDocument {
): TextDocument => {
let currentPos = 0;
const oldContent = document.getText();
let result = '';
Expand Down Expand Up @@ -258,18 +257,19 @@ function getEmbeddedDocument(
document.version,
result
);
}
};

function getPrefix(c: EmbeddedRegion) {
const getPrefix = (c: EmbeddedRegion) => {
if (c.attributeValue) {
switch (c.languageId) {
case 'css':
return CSS_STYLE_RULE + '{';
}
}
return '';
}
function getSuffix(c: EmbeddedRegion) {
};

const getSuffix = (c: EmbeddedRegion) => {
if (c.attributeValue) {
switch (c.languageId) {
case 'css':
Expand All @@ -279,16 +279,16 @@ function getSuffix(c: EmbeddedRegion) {
}
}
return '';
}
};

function substituteWithWhitespace(
const substituteWithWhitespace = (
result: string,
start: number,
end: number,
oldContent: string,
before: string,
after: string
) {
) => {
let accumulatedWS = 0;
result += before;
for (let i = start + before.length; i < end; i++) {
Expand All @@ -304,10 +304,9 @@ function substituteWithWhitespace(
result = append(result, ' ', accumulatedWS - after.length);
result += after;
return result;
}
};

// tslint:disable:no-bitwise
function append(result: string, str: string, n: number): string {
const append = (result: string, str: string, n: number): string => {
while (n > 0) {
if (n & 1) {
result += str;
Expand All @@ -316,13 +315,12 @@ function append(result: string, str: string, n: number): string {
str += str;
}
return result;
}
// tslint:enable:no-bitwise
};

function getAttributeLanguage(attributeName: string): string {
const getAttributeLanguage = (attributeName: string): string => {
const match = attributeName.match(/^(style)$|^(on\w+)$/i);
if (!match) {
return null;
}
return match[1] ? 'css' : 'javascript';
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import { applyEdits } from '../utils/edits';
import { isEOL } from '../utils/strings';
import { LanguageModes, Settings } from './languageModes';

export function format(
export const format = (
languageModes: LanguageModes,
document: TextDocument,
formatRange: Range,
formattingOptions: FormattingOptions,
settings: Settings,
enabledModes: { [mode: string]: boolean }
) {
) => {
const result: TextEdit[] = [];

const endPos = formatRange.end;
Expand Down Expand Up @@ -143,4 +143,4 @@ export function format(
} finally {
languageModes.onDocumentRemoved(newDocument);
}
}
};
Loading

0 comments on commit c2680df

Please sign in to comment.