Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion denops/ddx/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const main: Entrypoint = (denops: Denops) => {
if (ddxs[name].length == 0) {
ddxs[name].push(new Ddx(getLoader(name)));
}
return ddxs[name].slice(-1)[0];
return ddxs[name].at(-1)!;
};
const getLoader = (name: string) => {
if (!loaders[name]) {
Expand Down
21 changes: 12 additions & 9 deletions denops/ddx/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import { join } from "@std/path/join";
import { resolve } from "@std/path/resolve";
import { isAbsolute } from "@std/path/is-absolute";

// Probe once at module load time: avoids repeated TextDecoder construction
// inside #searchUtf8 which is called for every string-extraction request.
const FATAL_DECODER_SUPPORTED = (() => {
try {
new TextDecoder("utf-8", { fatal: true }).decode(new Uint8Array());
return true;
} catch {
return false;
}
})();

type OperationHistory =
| ChangeHistory
| ChangeBytesHistory
Expand Down Expand Up @@ -594,14 +605,6 @@ export class DdxBuffer {

const isUtf8Start = (b: number) => !(b >= 0x80 && b <= 0xbf);
let i = 0;
const fatalDecoderSupported = (() => {
try {
new TextDecoder("utf-8", { fatal: true }).decode(new Uint8Array());
return true;
} catch {
return false;
}
})();

while (i < n) {
if (!isUtf8Start(bytes[i])) {
Expand All @@ -612,7 +615,7 @@ export class DdxBuffer {
let lastGood = -1;
for (let j = i; j < n; j++) {
const slice = bytes.subarray(i, j + 1);
if (fatalDecoderSupported) {
if (FATAL_DECODER_SUPPORTED) {
const decoded = this.#tryDecodeFatal(slice, "utf-8");
if (decoded === null) break;
lastGood = j;
Expand Down
19 changes: 12 additions & 7 deletions denops/ddx/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ class Custom {
local: Record<string, Partial<DdxOptions>> = {};

get(userOptions: UserOptions): DdxOptions {
const options = foldMerge(mergeDdxOptions, defaultDdxOptions, [
this.global,
userOptions,
]);
const name = options.name;
// Extract the effective name cheaply without a full merge pass.
// userOptions is Record<string, unknown> so bracket notation is used;
// this.global is Partial<DdxOptions> so dot notation is appropriate there.
const name =
(userOptions["name"] ?? this.global.name ?? "default") as string;
const local = this.local[name] || {};
return foldMerge(mergeDdxOptions, defaultDdxOptions, [
this.global,
Expand Down Expand Up @@ -179,11 +179,16 @@ export class ContextBuilder {

await this.validate(denops, "options", userOptions, defaultDdxOptions());

const [bufNr, winId] = await Promise.all([
fn.bufnr(denops, "%"),
fn.win_getid(denops),
]);

return [
{
...defaultContext(),
bufNr: await fn.bufnr(denops, "%"),
winId: await fn.win_getid(denops) as number,
bufNr: bufNr as number,
winId: winId as number,
},
userOptions,
];
Expand Down
16 changes: 12 additions & 4 deletions denops/ddx/ddx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ export class Ddx {
}

try {
const [expandedPath, cwd] = await Promise.all([
fn.expand(denops, this.#options.path) as Promise<string>,
fn.getcwd(denops),
]);
await this.#buffer.open(
await fn.expand(denops, this.#options.path) as string,
await fn.getcwd(denops),
expandedPath,
cwd,
Number(this.#options.offset),
Number(this.#options.length),
);
Expand All @@ -84,9 +88,13 @@ export class Ddx {

if (this.#options.anotherPath.length > 0) {
try {
const [expandedAnotherPath, cwd] = await Promise.all([
fn.expand(denops, this.#options.anotherPath) as Promise<string>,
fn.getcwd(denops),
]);
await this.#anotherBuffer.open(
await fn.expand(denops, this.#options.anotherPath) as string,
await fn.getcwd(denops),
expandedAnotherPath,
cwd,
Number(this.#options.offset),
Number(this.#options.length),
);
Expand Down
9 changes: 7 additions & 2 deletions denops/ddx/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ function getCp932Decoder(): TextDecoder {
return new TextDecoder();
}

// Memoized CP932 decoder: constructed once, reused for every multibyte pair.
// TextDecoder.decode() without the `stream` option resets any internal state
// after each call, so reusing the instance is safe in a single-threaded
// JavaScript runtime (Deno does not share module-scope state across Workers).
const CP932_DECODER = getCp932Decoder();

export function bytesToCP932(buf: Uint8Array): string {
const out: string[] = [];
let i = 0;
Expand Down Expand Up @@ -60,8 +66,7 @@ export function bytesToCP932(buf: Uint8Array): string {

if (isValidShiftJisMultibyte(byte1, byte2)) {
try {
const decoder = getCp932Decoder();
const decoded = decoder.decode(new Uint8Array([byte1, byte2]));
const decoded = CP932_DECODER.decode(new Uint8Array([byte1, byte2]));

// Replace the decoded character if it's a replacement character
if (decoded === "�") {
Expand Down
Loading