Skip to content

Conversation

@Onokaev
Copy link

@Onokaev Onokaev commented Feb 14, 2023

Adds language support

Notes
Powershell and Go snippet generation currently not working.

Testing instructions
After typing a URL e.g https://graph.microsoft.com/me/messages,
Add a '-' at the end of the URL to get code snippets like so: https://graph.microsoft.com/me/messages-
Press Enter or tab to populate the editor with the code snippet

@Onokaev Onokaev marked this pull request as draft February 14, 2023 09:11
@estruyf estruyf marked this pull request as ready for review February 15, 2023 08:27
@estruyf
Copy link
Owner

estruyf commented Feb 15, 2023

Sorry @Onokaev that was by accident, you can keep on working on this PR.

I wanted to actually write a comment about the snippet. Like the idea, but it might be better to add a snippet by checking which URL you have in the clipboard or is present on the current line. As typing an extra character is not the best UX in my opinion.

Keep up the good work. Let me know when its finished and will be happy to review and merge it.

@Onokaev Onokaev marked this pull request as draft February 15, 2023 13:28
@estruyf
Copy link
Owner

estruyf commented Feb 20, 2023

Hey @Onokaev how did the hackathon go? How far did you get with this PR?

@Onokaev
Copy link
Author

Onokaev commented Feb 21, 2023

Hey @estruyf. Hackathon was awesome!
I am working on a different method of displaying the snippets without having to add a different character to trigger the snippet call.

The idea is to have a different Completion provider that checks if there are any active completion items in the editor. At this point, it's safe to return a snippet completion item

@Onokaev
Copy link
Author

Onokaev commented Feb 21, 2023

`
const url = require('native-url');
import { CancellationToken, CompletionItem, CompletionItemKind, CompletionItemProvider, MarkdownString, Position, TextDocument, ExtensionContext, CompletionContext, CompletionList, ProviderResult, languages, commands } from 'vscode';
import { autocompleteProviderHasITems } from './AutoCompleteProvider';
import { SnippetProvider } from './SnippetProvider';

export class SnippetCompletionProvider implements CompletionItemProvider {
private name: string = 'snippetProvider';

public async provideCompletionItems(document: TextDocument, position: Position, token: CancellationToken): Promise<CompletionItem[]> {
    const existingItems: any = await commands.executeCommand('vscode.executeCompletionItemProvider', document.uri, position, this.constructor.name);
    // Check if there are any existing items
    console.log('Existing items are ', existingItems);
    if (existingItems && existingItems.items && existingItems.items.length > 0) {
        console.log('We are returning because of stuff');
        if (existingItems.provider && existingItems.provider !== this.constructor.name) {
            return existingItems;
          }
    }

    let currentLine = document.getText(document.lineAt(position).range);
    console.log('Something happening ', currentLine);
    // Stripping out the text of where you placed the slash
    currentLine = currentLine.substring(0, position.character);

    return this.getSnippets(currentLine);
}

private async getSnippets(currentLine: String): Promise<CompletionItem[]>{
    const splicedPath = currentLine.slice(0,-1);
      const snippetObject = await SnippetProvider.initialize(splicedPath);
      const snippet = snippetObject.getSnippet();
      const snippetCompletionItem = new CompletionItem(snippet);
      snippetCompletionItem.documentation = snippet;
      snippetCompletionItem.insertText = "\n" + snippet;
      return [snippetCompletionItem];
  }

}`

This is what I have tried so far, but it appears to get into an infinite loop

@Onokaev
Copy link
Author

Onokaev commented Feb 21, 2023

My friend suggested another approach that still uses more characters to trigger a snippet call. The idea is to decouple the snippet completion item from the autocomplete item like so:
`
https://graph.microsoft.com/v1.0/me (This should just provide autocomplete items)

gsn https://graph.microsoft.com/v1.0/me (This should provide snippet completion items because of the preceding gsn)
`

@Onokaev
Copy link
Author

Onokaev commented Feb 21, 2023

I could really use more ideas :)

@estruyf
Copy link
Owner

estruyf commented Feb 21, 2023

What if you would do something like the copilot does it?

image

Another option is to open a virtual document at the side, which shows snippet options.

@Onokaev
Copy link
Author

Onokaev commented Feb 23, 2023

Sure. Let me try a virtual document

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants