Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👍 remove unnecessary 'foldmethod' changes #271

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 5 additions & 16 deletions buffer/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,16 @@ async function ensurePrerequisites(denops: Denops): Promise<string> {
function! DenopsStdBufferAppend_${suffix}(bufnr, lnum, repl) abort
let modified = getbufvar(a:bufnr, '&modified')
let modifiable = getbufvar(a:bufnr, '&modifiable')
let foldmethod = getbufvar(a:bufnr, '&foldmethod')
call setbufvar(a:bufnr, '&modifiable', 1)
call setbufvar(a:bufnr, '&foldmethod', 'manual')
call appendbufline(a:bufnr, a:lnum, a:repl)
call setbufvar(a:bufnr, '&modified', modified)
call setbufvar(a:bufnr, '&modifiable', modifiable)
call setbufvar(a:bufnr, '&foldmethod', foldmethod)
endfunction

function! DenopsStdBufferReplace_${suffix}(bufnr, repl, fileformat, fileencoding) abort
let modified = getbufvar(a:bufnr, '&modified')
let modifiable = getbufvar(a:bufnr, '&modifiable')
let foldmethod = getbufvar(a:bufnr, '&foldmethod')
call setbufvar(a:bufnr, '&modifiable', 1)
call setbufvar(a:bufnr, '&foldmethod', 'manual')
if a:fileformat isnot# v:null
call setbufvar(a:bufnr, '&fileformat', a:fileformat)
endif
Expand All @@ -82,7 +77,6 @@ async function ensurePrerequisites(denops: Denops): Promise<string> {
call deletebufline(a:bufnr, len(a:repl) + 1, '$')
call setbufvar(a:bufnr, '&modified', modified)
call setbufvar(a:bufnr, '&modifiable', modifiable)
call setbufvar(a:bufnr, '&foldmethod', foldmethod)
endfunction

function! DenopsStdBufferConcreteRestore_${suffix}() abort
Expand Down Expand Up @@ -321,7 +315,7 @@ export interface DecodeResult {
* }
* ```
*
* It temporary change `modified`, `modifiable`, and `foldmethod` options to append
* It temporary change `modified` and `modifiable` options to append
* the content of the `buffer` buffer without unmodifiable error or so on.
*/
export async function append(
Expand Down Expand Up @@ -361,8 +355,8 @@ export interface AppendOptions {
* }
* ```
*
* It temporary change `modified`, `modifiable`, and `foldmethod` options to
* replace the content of the `buffer` buffer without unmodifiable error or so on.
* It temporary change `modified` and `modifiable` options to replace
* the content of the `buffer` buffer without unmodifiable error or so on.
*/
export async function replace(
denops: Denops,
Expand Down Expand Up @@ -518,25 +512,20 @@ export async function modifiable<T>(
bufnr: number,
executor: () => T,
): Promise<T> {
const [modified, modifiable, foldmethod] = await batch.collect(
const [modified, modifiable] = await batch.collect(
denops,
(denops) => [
op.modified.getBuffer(denops, bufnr),
op.modifiable.getBuffer(denops, bufnr),
op.foldmethod.getBuffer(denops, bufnr),
],
);
await batch.batch(denops, async (denops) => {
await fn.setbufvar(denops, bufnr, "&modifiable", 1);
await fn.setbufvar(denops, bufnr, "&foldmethod", "manual");
});
await fn.setbufvar(denops, bufnr, "&modifiable", 1);
try {
return await executor();
} finally {
await batch.batch(denops, async (denops) => {
await fn.setbufvar(denops, bufnr, "&modified", modified);
await fn.setbufvar(denops, bufnr, "&modifiable", modifiable);
await fn.setbufvar(denops, bufnr, "&foldmethod", foldmethod);
});
}
}
99 changes: 99 additions & 0 deletions buffer/buffer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,69 @@ test({
assertEquals(0, await fn.getbufvar(denops, bufnr, "&modifiable"));
},
});
await t.step({
name: "appends content of a 'foldmethod=marker' buffer",
fn: async () => {
await denops.cmd("enew");
const bufnr = await fn.bufnr(denops);
await fn.setbufvar(denops, bufnr, "&foldmethod", "marker");
await fn.setbufvar(denops, bufnr, "&foldmarker", "{{{,}}}");
await append(denops, bufnr, [
"Hello {{{",
"Darkness",
"My }}}",
"Old friend",
]);
assertEquals([
"",
"Hello {{{",
"Darkness",
"My }}}",
"Old friend",
], await fn.getline(denops, 1, "$"));
assertEquals(
await fn.getbufvar(denops, bufnr, "&foldmethod"),
"marker",
);

await fn.setbufvar(denops, bufnr, "&foldlevel", 0);
await append(denops, bufnr, [
"Joking",
]);
assertEquals([
"",
"Joking",
"Hello {{{",
"Darkness",
"My }}}",
"Old friend",
], await fn.getline(denops, 1, "$"));
assertEquals(
await fn.getbufvar(denops, bufnr, "&foldmethod"),
"marker",
);

await fn.setbufvar(denops, bufnr, "&foldlevel", 0);
await append(denops, bufnr, [
"Foo",
], {
lnum: 3,
});
assertEquals([
"",
"Joking",
"Hello {{{",
"Foo",
"Darkness",
"My }}}",
"Old friend",
], await fn.getline(denops, 1, "$"));
assertEquals(
await fn.getbufvar(denops, bufnr, "&foldmethod"),
"marker",
);
},
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: New test case for appending content with 'foldmethod=marker'

This test case is well-structured and covers the functionality of appending content to a buffer with the foldmethod set to "marker". It checks both the content of the buffer and the state of the foldmethod after operations, which aligns with the PR's objective to ensure functionality remains intact after removing unnecessary foldmethod changes.

However, the test could be enhanced by verifying the actual folding behavior in the buffer, not just the foldmethod setting. This would ensure that the folding functionality itself is not affected by the changes.

Would you like me to help add assertions to verify the actual folding behavior within the buffer?

},
});

Expand Down Expand Up @@ -361,6 +424,42 @@ test({
assertEquals(0, await fn.getbufvar(denops, bufnr, "&modifiable"));
},
});
await t.step({
name: "replaces content of an 'foldmethod=marker' buffer",
fn: async () => {
const bufnr = await fn.bufnr(denops);
await fn.setbufvar(denops, bufnr, "&foldmethod", "marker");
await fn.setbufvar(denops, bufnr, "&foldmarker", "{{{,}}}");
await replace(denops, bufnr, [
"Hello {{{",
"Darkness",
"My }}}",
"Old friend",
]);
assertEquals([
"Hello {{{",
"Darkness",
"My }}}",
"Old friend",
], await fn.getline(denops, 1, "$"));
assertEquals(
await fn.getbufvar(denops, bufnr, "&foldmethod"),
"marker",
);

await fn.setbufvar(denops, bufnr, "&foldlevel", 0);
await replace(denops, bufnr, [
"Joking {{{1",
]);
assertEquals([
"Joking {{{1",
], await fn.getline(denops, 1, "$"));
assertEquals(
await fn.getbufvar(denops, bufnr, "&foldmethod"),
"marker",
);
},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: New test case for replacing content with 'foldmethod=marker'

This test case follows a similar structure to the appending test case, focusing on replacing content in a buffer with the foldmethod set to "marker". It effectively verifies that the content and foldmethod setting are correctly handled after the replace operation.

Like the previous test case, it would be beneficial to include checks for the actual folding behavior to ensure that the folding functionality is correctly preserved post-operation.

Would you like assistance in adding checks to verify the actual folding behavior after content replacement in the buffer?

});
},
});

Expand Down