mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-07 04:26:32 +08:00
Remove FormatterInterface
This class was a remnant of the code structure during the Closure Compiler era. In ES modules, we no longer need a special class and instead can explicitly export the relevant functions and remove the interim class. R=szuend@chromium.org Bug: none Change-Id: Ib6a2bcaef9a1dad462f02a3411b9bef1ee989a34 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3053747 Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org> Reviewed-by: Simon Zünd <szuend@chromium.org>
This commit is contained in:
committed by
Devtools-frontend LUCI CQ
parent
312a8ea35e
commit
3c3cd80fc6
@@ -36,42 +36,40 @@ import * as Platform from '../../core/platform/platform.js';
|
||||
import type {FormatMapping, FormatResult} from './FormatterWorkerPool.js';
|
||||
import {formatterWorkerPool} from './FormatterWorkerPool.js';
|
||||
|
||||
export class FormatterInterface {
|
||||
static format(
|
||||
contentType: Common.ResourceType.ResourceType, mimeType: string, content: string,
|
||||
callback: (arg0: string, arg1: FormatterSourceMapping) => Promise<void>): void {
|
||||
if (contentType.isDocumentOrScriptOrStyleSheet()) {
|
||||
new ScriptFormatter(mimeType, content, callback);
|
||||
} else {
|
||||
new ScriptIdentityFormatter(mimeType, content, callback);
|
||||
}
|
||||
function locationToPosition(lineEndings: number[], lineNumber: number, columnNumber: number): number {
|
||||
const position = lineNumber ? lineEndings[lineNumber - 1] + 1 : 0;
|
||||
return position + columnNumber;
|
||||
}
|
||||
|
||||
function positionToLocation(lineEndings: number[], position: number): number[] {
|
||||
const lineNumber =
|
||||
Platform.ArrayUtilities.upperBound(lineEndings, position - 1, Platform.ArrayUtilities.DEFAULT_COMPARATOR);
|
||||
let columnNumber;
|
||||
if (!lineNumber) {
|
||||
columnNumber = position;
|
||||
} else {
|
||||
columnNumber = position - lineEndings[lineNumber - 1] - 1;
|
||||
}
|
||||
static locationToPosition(lineEndings: number[], lineNumber: number, columnNumber: number): number {
|
||||
const position = lineNumber ? lineEndings[lineNumber - 1] + 1 : 0;
|
||||
return position + columnNumber;
|
||||
}
|
||||
static positionToLocation(lineEndings: number[], position: number): number[] {
|
||||
const lineNumber =
|
||||
Platform.ArrayUtilities.upperBound(lineEndings, position - 1, Platform.ArrayUtilities.DEFAULT_COMPARATOR);
|
||||
let columnNumber;
|
||||
if (!lineNumber) {
|
||||
columnNumber = position;
|
||||
} else {
|
||||
columnNumber = position - lineEndings[lineNumber - 1] - 1;
|
||||
}
|
||||
return [lineNumber, columnNumber];
|
||||
return [lineNumber, columnNumber];
|
||||
}
|
||||
|
||||
export function format(
|
||||
contentType: Common.ResourceType.ResourceType, mimeType: string, content: string,
|
||||
callback: (arg0: string, arg1: FormatterSourceMapping) => Promise<void>): void {
|
||||
if (contentType.isDocumentOrScriptOrStyleSheet()) {
|
||||
new ScriptFormatter(mimeType, content, callback);
|
||||
} else {
|
||||
new ScriptIdentityFormatter(mimeType, content, callback);
|
||||
}
|
||||
}
|
||||
|
||||
export class ScriptFormatter extends FormatterInterface {
|
||||
export class ScriptFormatter {
|
||||
_mimeType: string;
|
||||
_originalContent: string;
|
||||
_callback: (arg0: string, arg1: FormatterSourceMapping) => Promise<void>;
|
||||
|
||||
constructor(
|
||||
mimeType: string, content: string, callback: (arg0: string, arg1: FormatterSourceMapping) => Promise<void>) {
|
||||
super();
|
||||
|
||||
this._mimeType = mimeType;
|
||||
this._originalContent = content.replace(/\r\n?|[\n\u2028\u2029]/g, '\n').replace(/^\uFEFF/, '');
|
||||
this._callback = callback;
|
||||
@@ -101,11 +99,9 @@ export class ScriptFormatter extends FormatterInterface {
|
||||
}
|
||||
}
|
||||
|
||||
class ScriptIdentityFormatter extends FormatterInterface {
|
||||
class ScriptIdentityFormatter {
|
||||
constructor(
|
||||
mimeType: string, content: string, callback: (arg0: string, arg1: FormatterSourceMapping) => Promise<void>) {
|
||||
super();
|
||||
|
||||
callback(content, new IdentityFormatterSourceMapping());
|
||||
}
|
||||
}
|
||||
@@ -139,18 +135,16 @@ class FormatterSourceMappingImpl extends FormatterSourceMapping {
|
||||
}
|
||||
|
||||
originalToFormatted(lineNumber: number, columnNumber?: number): number[] {
|
||||
const originalPosition =
|
||||
FormatterInterface.locationToPosition(this._originalLineEndings, lineNumber, columnNumber || 0);
|
||||
const originalPosition = locationToPosition(this._originalLineEndings, lineNumber, columnNumber || 0);
|
||||
const formattedPosition =
|
||||
this._convertPosition(this._mapping.original, this._mapping.formatted, originalPosition || 0);
|
||||
return FormatterInterface.positionToLocation(this._formattedLineEndings, formattedPosition);
|
||||
return positionToLocation(this._formattedLineEndings, formattedPosition);
|
||||
}
|
||||
|
||||
formattedToOriginal(lineNumber: number, columnNumber?: number): number[] {
|
||||
const formattedPosition =
|
||||
FormatterInterface.locationToPosition(this._formattedLineEndings, lineNumber, columnNumber || 0);
|
||||
const formattedPosition = locationToPosition(this._formattedLineEndings, lineNumber, columnNumber || 0);
|
||||
const originalPosition = this._convertPosition(this._mapping.formatted, this._mapping.original, formattedPosition);
|
||||
return FormatterInterface.positionToLocation(this._originalLineEndings, originalPosition || 0);
|
||||
return positionToLocation(this._originalLineEndings, originalPosition || 0);
|
||||
}
|
||||
|
||||
_convertPosition(positions1: number[], positions2: number[], position: number): number {
|
||||
|
||||
@@ -11,7 +11,7 @@ import * as TextUtils from '../text_utils/text_utils.js';
|
||||
import * as Workspace from '../workspace/workspace.js';
|
||||
|
||||
import type {FormatterSourceMapping} from './ScriptFormatter.js';
|
||||
import {FormatterInterface} from './ScriptFormatter.js';
|
||||
import {format} from './ScriptFormatter.js';
|
||||
|
||||
const objectToFormattingResult = new WeakMap<Object, SourceFormatData>();
|
||||
|
||||
@@ -119,7 +119,7 @@ export class SourceFormatter {
|
||||
const resultPromise = new Promise<SourceFormatData>(async resolve => {
|
||||
const {content} = await uiSourceCode.requestContent();
|
||||
|
||||
FormatterInterface.format(
|
||||
format(
|
||||
uiSourceCode.contentType(), uiSourceCode.mimeType(), content || '',
|
||||
async (formattedContent, formatterMapping) => {
|
||||
const cacheEntry = this._formattedSourceCodes.get(uiSourceCode);
|
||||
|
||||
@@ -14,9 +14,6 @@ Formatter.FormatterWorkerPool = FormatterModule.FormatterWorkerPool.FormatterWor
|
||||
|
||||
Formatter.formatterWorkerPool = FormatterModule.FormatterWorkerPool.formatterWorkerPool;
|
||||
|
||||
/** @interface */
|
||||
Formatter.Formatter = FormatterModule.ScriptFormatter.FormatterInterface;
|
||||
|
||||
/** @constructor */
|
||||
Formatter.ScriptFormatter = FormatterModule.ScriptFormatter.ScriptFormatter;
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ export class InplaceFormatterEditorAction implements EditorAction {
|
||||
|
||||
_contentLoaded(uiSourceCode: Workspace.UISourceCode.UISourceCode, content: string): void {
|
||||
const highlighterType = uiSourceCode.mimeType();
|
||||
Formatter.ScriptFormatter.FormatterInterface.format(
|
||||
Formatter.ScriptFormatter.format(
|
||||
uiSourceCode.contentType(), highlighterType, content, async (formattedContent, formatterMapping) => {
|
||||
this._formattingComplete(uiSourceCode, formattedContent, formatterMapping);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user