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

feat: Add code action ({# fmt:off #} & {# fmt:on #}) #3

Merged
merged 1 commit into from
Jun 5, 2021
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ django templates (htmldjango) extension for [coc.nvim](https://github.com/neocli
- by [Unibeautify](https://unibeautify.com/) (Beautifiers: JS-Beautify + Pretty Diff) [Node tool]
- by [DjHTML](https://github.com/rtts/djhtml) ("Django/Jinja" template indenter) [Python tool] | [DEMO](https://github.com/yaegassy/coc-htmldjango/pull/2)
- Hover | [DEMO](https://github.com/yaegassy/coc-htmldjango/pull/1)
- CodeAction
- Snippets completion
- To use it, you need to install [coc-snippets](https://github.com/neoclide/coc-snippets).
- And set `snippets.loadFromExtensions` to true in "coc-settings.json"
Expand Down Expand Up @@ -71,6 +72,11 @@ Plug 'yaegassy/coc-htmldjango', {'do': 'yarn install --frozen-lockfile'}
- `htmldjango.djhtml.format`
- `htmldjango.djhtml.install`

## Code Actions

- `Add {# fmt:off #} for this line`
- `Add {# fmt:on #} for this line`

## Bult-in install (DjHTML)

coc-htmldjango allows you to create an extension-only "venv" and install "djhtml".
Expand Down
83 changes: 83 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
CodeAction,
CodeActionContext,
CodeActionProvider,
Position,
Range,
TextEdit,
TextDocument,
workspace,
} from 'coc.nvim';

export class HtmlDjangoCodeActionProvider implements CodeActionProvider {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async provideCodeActions(document: TextDocument, range: Range, context: CodeActionContext) {
const doc = workspace.getDocument(document.uri);
const wholeRange = Range.create(0, 0, doc.lineCount, 0);
let whole = false;
if (
range.start.line === wholeRange.start.line &&
range.start.character === wholeRange.start.character &&
range.end.line === wholeRange.end.line &&
range.end.character === wholeRange.end.character
) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
whole = true;
}
const codeActions: CodeAction[] = [];

/** Add {# fmt:off #} for this line (htmldjango) */
if (range.start.line === range.end.line && range.start.character === 0) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const line = doc.getline(range.start.line);

const thisLineFullLength = doc.getline(range.start.line).length;
const thisLineTrimLength = doc.getline(range.start.line).trim().length;
const suppressLineLength = thisLineFullLength - thisLineTrimLength;

let suppressLineNewText = '{# fmt:off #}\n';
if (suppressLineLength > 0) {
const addIndentSpace = ' '.repeat(suppressLineLength);
suppressLineNewText = '{# fmt:off #}\n' + addIndentSpace;
}

const edit = TextEdit.insert(Position.create(range.start.line, suppressLineLength), suppressLineNewText);
codeActions.push({
title: 'Add {# fmt:off #} for this line',
edit: {
changes: {
[doc.uri]: [edit],
},
},
});
}

/** Add {# fmt:on #} for this line (htmldjango) */
if (range.start.line === range.end.line && range.start.character === 0) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const line = doc.getline(range.start.line);

const thisLineFullLength = doc.getline(range.start.line).length;
const thisLineTrimLength = doc.getline(range.start.line).trim().length;
const suppressLineLength = thisLineFullLength - thisLineTrimLength;

let suppressLineNewText = '{# fmt:on #}\n';
if (suppressLineLength > 0) {
const addIndentSpace = ' '.repeat(suppressLineLength);
suppressLineNewText = '{# fmt:on #}\n' + addIndentSpace;
}

const edit = TextEdit.insert(Position.create(range.start.line, suppressLineLength), suppressLineNewText);
codeActions.push({
title: 'Add {# fmt:on #} for this line',
edit: {
changes: {
[doc.uri]: [edit],
},
},
});
}

return codeActions;
}
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import HtmlDjangoFormattingEditProvider, { fullDocumentRange } from './format';
import { HtmlDjangoHoverProvider } from './hover';
import { djhtmlInstall } from './installer';
import { resolveDjhtmlPath } from './tool';
import { HtmlDjangoCodeActionProvider } from './action';

interface Selectors {
rangeLanguageSelector: DocumentSelector;
Expand Down Expand Up @@ -128,6 +129,10 @@ export async function activate(context: ExtensionContext): Promise<void> {
);

context.subscriptions.push(languages.registerHoverProvider(['htmldjango'], new HtmlDjangoHoverProvider(context)));

const languageSelector: DocumentSelector = [{ language: 'htmldjango', scheme: 'file' }];
const codeActionProvider = new HtmlDjangoCodeActionProvider();
context.subscriptions.push(languages.registerCodeActionProvider(languageSelector, codeActionProvider, 'htmldjango'));
}

async function installWrapper(pythonCommand: string, context: ExtensionContext) {
Expand Down