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

Support for AsciiDoc fenced code blocks #938

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ REST Client allows you to send HTTP request and view the response in Visual Stud
- Support navigate to symbol definitions(request and file level custom variable) in open `http` file
- CodeLens support to add an actionable link to send request
- Fold/Unfold for request block
* Support for AsciiDoc fenced code blocks

## Usage
In editor, type an HTTP request as simple as below:
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"onCommand:rest-client.copy-request-as-curl",
"onCommand:rest-client.fold-response",
"onCommand:rest-client.unfold-response",
"onLanguage:http"
"onLanguage:http",
"onLanguage:asciidoc"
],
"main": "./dist/extension",
"contributes": {
Expand Down
35 changes: 22 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { commands, ExtensionContext, languages, Range, TextDocument, Uri, window, workspace } from 'vscode';
import { commands, DocumentSelector, ExtensionContext, languages, Range, TextDocument, Uri, window, workspace } from 'vscode';
import { CodeSnippetController } from './controllers/codeSnippetController';
import { EnvironmentController } from './controllers/environmentController';
import { HistoryController } from './controllers/historyController';
import { RequestController } from './controllers/requestController';
import { AsciidocCodeLensProvider } from './providers/asciidocCodeLensProvider';
import { CustomVariableDiagnosticsProvider } from "./providers/customVariableDiagnosticsProvider";
import { RequestBodyDocumentLinkProvider } from './providers/documentLinkProvider';
import { EnvironmentOrFileVariableHoverProvider } from './providers/environmentOrFileVariableHoverProvider';
Expand Down Expand Up @@ -50,27 +51,35 @@ export async function activate(context: ExtensionContext) {
});
}));

const documentSelector = [
const restDocumentSelector: DocumentSelector = [
{ language: 'http', scheme: '*' }
];

context.subscriptions.push(languages.registerCompletionItemProvider(documentSelector, new HttpCompletionItemProvider()));
context.subscriptions.push(languages.registerCompletionItemProvider(documentSelector, new RequestVariableCompletionItemProvider(), '.'));
context.subscriptions.push(languages.registerHoverProvider(documentSelector, new EnvironmentOrFileVariableHoverProvider()));
context.subscriptions.push(languages.registerHoverProvider(documentSelector, new RequestVariableHoverProvider()));
const asciidocDocumentSelector: DocumentSelector = [
{ language: 'asciidoc', scheme: '*' }
];

context.subscriptions.push(languages.registerCompletionItemProvider(restDocumentSelector, new HttpCompletionItemProvider()));
context.subscriptions.push(languages.registerCompletionItemProvider(restDocumentSelector, new RequestVariableCompletionItemProvider(), '.'));
context.subscriptions.push(languages.registerHoverProvider(restDocumentSelector, new EnvironmentOrFileVariableHoverProvider()));
context.subscriptions.push(languages.registerHoverProvider(restDocumentSelector, new RequestVariableHoverProvider()));
context.subscriptions.push(
new ConfigurationDependentRegistration(
() => languages.registerCodeLensProvider(documentSelector, new HttpCodeLensProvider()),
() => languages.registerCodeLensProvider(restDocumentSelector, new HttpCodeLensProvider()),
s => s.enableSendRequestCodeLens));
context.subscriptions.push(
new ConfigurationDependentRegistration(
() => languages.registerCodeLensProvider(documentSelector, new FileVariableReferencesCodeLensProvider()),
() => languages.registerCodeLensProvider(restDocumentSelector, new FileVariableReferencesCodeLensProvider()),
s => s.enableCustomVariableReferencesCodeLens));
context.subscriptions.push(languages.registerDocumentLinkProvider(documentSelector, new RequestBodyDocumentLinkProvider()));
context.subscriptions.push(languages.registerDefinitionProvider(documentSelector, new FileVariableDefinitionProvider()));
context.subscriptions.push(languages.registerDefinitionProvider(documentSelector, new RequestVariableDefinitionProvider()));
context.subscriptions.push(languages.registerReferenceProvider(documentSelector, new FileVariableReferenceProvider()));
context.subscriptions.push(languages.registerDocumentSymbolProvider(documentSelector, new HttpDocumentSymbolProvider()));
context.subscriptions.push(
new ConfigurationDependentRegistration(
() => languages.registerCodeLensProvider(asciidocDocumentSelector, new AsciidocCodeLensProvider()),
s => s.enableSendRequestCodeLens));
context.subscriptions.push(languages.registerDocumentLinkProvider(restDocumentSelector, new RequestBodyDocumentLinkProvider()));
context.subscriptions.push(languages.registerDefinitionProvider(restDocumentSelector, new FileVariableDefinitionProvider()));
context.subscriptions.push(languages.registerDefinitionProvider(restDocumentSelector, new RequestVariableDefinitionProvider()));
context.subscriptions.push(languages.registerReferenceProvider(restDocumentSelector, new FileVariableReferenceProvider()));
context.subscriptions.push(languages.registerDocumentSymbolProvider(restDocumentSelector, new HttpDocumentSymbolProvider()));

const diagnosticsProvider = new CustomVariableDiagnosticsProvider();
context.subscriptions.push(diagnosticsProvider);
Expand Down
22 changes: 22 additions & 0 deletions src/providers/asciidocCodeLensProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CancellationToken, CodeLens, CodeLensProvider, Command, Range, TextDocument } from 'vscode';
import { Selector } from '../utils/selector';

export class AsciidocCodeLensProvider implements CodeLensProvider {

public provideCodeLenses(document: TextDocument, _token: CancellationToken): Promise<CodeLens[]> {
const blocks: CodeLens[] = [];

for (const range of Selector.getAsciidocRestSnippets(document)) {
const snippetRange = new Range(range.start.line + 2, 0, range.end.line, 0);
const cmd: Command = {
arguments: [document, snippetRange],
title: 'Send Request',
command: 'rest-client.request',
};
blocks.push(new CodeLens(range, cmd));
}

return Promise.resolve(blocks);
}

}
47 changes: 44 additions & 3 deletions src/utils/selector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EOL } from 'os';
import { Range, TextEditor } from 'vscode';
import { Position, Range, TextDocument, TextEditor } from 'vscode';
import * as Constants from '../common/constants';
import Logger from '../logger';
import { fromString as ParseReqMetaKey, RequestMetadata } from '../models/requestMetadata';
import { SelectedRequest } from '../models/SelectedRequest';
import { VariableProcessor } from './variableProcessor';
Expand All @@ -23,7 +24,22 @@ export class Selector {
let selectedText: string | null;
if (editor.selection.isEmpty || range) {
const activeLine = range?.start.line ?? editor.selection.active.line;
selectedText = this.getDelimitedText(editor.document.getText(), activeLine);

Logger.info('language', editor.document.languageId);

if (editor.document.languageId === 'asciidoc') {
selectedText = null;

for (const r of Selector.getAsciidocRestSnippets(editor.document)) {
const snippetRange = new Range(r.start.line + 2, 0, r.end.line, 0);
if (snippetRange.contains(new Position(activeLine, 0))) {
selectedText = editor.document.getText(snippetRange);
}
}

} else {
selectedText = this.getDelimitedText(editor.document.getText(), activeLine);
}
} else {
selectedText = editor.document.getText(editor.selection);
}
Expand Down Expand Up @@ -189,4 +205,29 @@ export class Selector {
.filter(([, value]) => /^#{3,}/.test(value))
.map(([index, ]) => +index);
}
}

public static* getAsciidocRestSnippets(document: TextDocument): Generator<Range> {
const snippetStartRegx = /^\[source,http\]$/;
const snippetDelimiterRegx = /^----$/;

let snippetStart: number | undefined;
for (let i = 0; i < document.lineCount; i++) {
const lineText = document.lineAt(i).text;

const matchEnd = lineText.match(snippetDelimiterRegx);
if (snippetStart !== undefined && matchEnd && i !== snippetStart + 1) {
const snippetEnd = i;

const range = new Range(snippetStart, 0, snippetEnd, 0);
yield range;

snippetStart = undefined;
} else {
const matchStart = lineText.match(snippetStartRegx);
if (matchStart && document.lineAt(i + 1)?.text?.match(snippetDelimiterRegx)) {
snippetStart = i;
}
}
}
}
}