Skip to content

Commit bb1ca0b

Browse files
Hackathon - Quickstart (#761)
<!-- Explain the changes introduced in your PR --> ## Pull Request approval You will need to get your PR approved by at least one member of the Sourcegraph team. For reviews of docs formatting, styles, and component usage, please tag the docs team via the #docs Slack channel. --------- Co-authored-by: Maedah Batool <[email protected]>
1 parent a11d467 commit bb1ca0b

File tree

1 file changed

+41
-104
lines changed

1 file changed

+41
-104
lines changed

docs/cody/quickstart.mdx

Lines changed: 41 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,75 @@
11
# Cody Quickstart
22

3-
<p className="subtitle">In this quickstart guide, you'll learn how to use Cody once you have installed the extension in your VS Code editor. Here you will perform the following three tasks:</p>
3+
<p className="subtitle">This quickstart guide shows how to use Cody in the VS Code editor. You'll learn about the following tasks:</p>
44

5-
1. Generate unit tests for your code
6-
2. Identify errors and get suggested code fixes
7-
3. Add documentation to your code snippets
5+
- Chat with Cody to ask questions about your code
6+
- Code completions and suggestions
7+
- Use Cody to refactor code
8+
- Use Cody to debug code and ask Cody to fix bugs
89

9-
## Prerequisites
10-
11-
- Make sure you have the [Cody extension installed](/cody/clients/install-vscode) in your VS Code editor
12-
- You have a Free or Pro account through Sourcegraph.com or a Sourcegraph Enterprise account
13-
- You have a project open in VS Code
14-
15-
## Getting started with the Cody extension, prompts, and commands
16-
17-
After installing the extension, the side activity bar will display an icon for **Cody**. Click this icon, and Cody's panel will open. This interface is used to start a **New Chat**, run Cody **commands**, prompts or get access to relevant resources.
18-
19-
![Cody icon in side activity bar ](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-sidebar-102024.png)
20-
21-
Cody supports **Prompts** and **Commands** with VS Code. These are quick, ready-to-use actions that you can apply to any code or text-based snippet you've highlighted. You can run a command in the following ways:
10+
<Callout type="info">There are [many ways to use Cody](/cody/clients). For this guide, we'll be using the [VS Code](/cody/clients/install-vscode) extension.</Callout>
2211

23-
- Highlight your code and select the command or any prompt from the sidebar
24-
- Press `Option+K` to run an edit command exclusively
25-
- Right-click on any code element and select **Cody > Choose a command** from the list
26-
27-
## Working with the Cody extension
28-
29-
Let's create a JavaScript function that converts a `given date` into a human-readable description of the time elapsed between the `given date` and the `current date`. This example uses a starter boilerplate code that helps you set up and run three tasks with the Cody VS Code extension.
30-
31-
Here's the code for the date elapsed function:
32-
33-
```js
34-
function getTimeAgoDescription(dateString) {
35-
const startDate = new Date(dateString);
36-
const currentDate = new Date();
37-
38-
const years = currentDate.getFullYear() - startDate.getFullYear();
39-
const months = currentDate.getMonth() - startDate.getMonth();
40-
const days = currentDate.getDate() - startDate.getDate();
12+
## Prerequisites
4113

42-
let timeAgoDescription = '';
14+
Before you start, you'll need the following:
4315

44-
if (years > 0) {
45-
timeAgoDescription += `${years} ${years === 1 ? 'year' : 'years'}`;
46-
}
16+
- [Cody extension installed](/cody/clients/install-vscode) in your VS Code editor
17+
- Free or Pro account via Sourcegraph.com or a Sourcegraph Enterprise account
18+
- A project open in VS Code
4719

48-
if (months > 0) {
49-
if (timeAgoDescription !== '') {
50-
timeAgoDescription += ' ';
51-
}
52-
timeAgoDescription += `${months} ${months === 1 ? 'month' : 'months'}`;
53-
}
20+
## Getting started with the Cody
5421

55-
if (days > 0) {
56-
if (timeAgoDescription !== '') {
57-
timeAgoDescription += ' ';
58-
}
59-
timeAgoDescription += `${days} ${days === 1 ? 'day' : 'days'}`;
60-
}
22+
After installing the extension and connecting to a Sourcegraph instance, you can leverage Cody to use the best LLM and context to understand, write, and fix code. Click the **Cody** icon from the VS Code side activity bar to open the Cody chat panel.
6123

62-
if (timeAgoDescription === '') {
63-
timeAgoDescription = 'today';
64-
} else {
65-
timeAgoDescription += ' ago';
66-
}
24+
By default, the chat input will have the context of your entire codebase, and Claude 3.5 Sonnet (New) is selected as the default chat model. Based on your [Cody tier](https://sourcegraph.com/pricing?product=cody), you can change the LLM model and context based on your use case to optimize speed, accuracy, or cost.
6725

68-
return timeAgoDescription;
69-
}
70-
```
26+
To help you automate your key tasks in your development workflow, you get **[Prompts](/cody/capabilities/commands)**. If you are a part of an organization on Sourcegraph.com or a self-hosted Sourcegraph instance, you can view these pre-built Prompts created by your teammates. On the contrary, you can create your own Prompts via the **Prompt Library** from your Sourcegraph instance.
7127

72-
## 1. Generate a unit test
28+
The Cody chat interface offers a few more options and settings. [You can read more in these docs](/cody/capabilities/chat).
7329

74-
To ensure code quality and early bug detection, one of the most useful commands that Cody offers is **Generate unit tests**. It quickly helps you write test code for any snippet that you have highlighted. To generate a unit test for our example function:
30+
![cody-chat-interface](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-chat-interface-1124.png)
7531

76-
- Open the `date.js` file in VS Code
77-
- Highlight a code snippet that you'd like to test
78-
- Inside Cody chat, type `/test` or press the command hotkey (`` + `c` / `alt` + `c`)
79-
- Select `Generate unit tests` option and hit `Enter`
32+
## Chat with Cody
8033

81-
Cody will help you generate the following unit test in the sidebar:
34+
The best way to see Cody in action is through chat. Cody allows you to chat directly with AI to ask questions about your code. You can start by asking simple questions like:
8235

83-
```js
84-
import assert from 'assert';
85-
import { getTimeAgoDescription } from '../src/date.js';
36+
- What does this code do?
37+
- Explain this codebase in three to four lines
8638

87-
describe('getTimeAgoDescription', () => {
88-
it('returns correct relative time for date 1 month ago', () => {
89-
const oneMonthAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
90-
const result = getTimeAgoDescription(oneMonthAgo);
91-
assert.equal(result, '1 month ago');
92-
});
39+
Or ask more complex questions like:
9340

94-
it('returns correct relative time for date 2 months ago', () => {
95-
const twoMonthsAgo = new Date(Date.now() - 2 * 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
96-
const result = getTimeAgoDescription(twoMonthsAgo);
97-
assert.equal(result, '2 months ago');
98-
});
99-
});
41+
- Suggest ways to refactor the codebase?
42+
- How can I make this code more performant?
10043

101-
```
44+
Cody will respond with a code snippet, a suggested fix, or an explanation.
10245

103-
This unit test file tests the `getTimeAgoDescription()` function from the `date.js` file. These tests help you validate that the `getTimeAgoDescription()` function correctly returns the relative time descriptions for dates in the past. The tests generate specific sample input dates and confirm that the output matches the expected result.
46+
![chat-with-cody](https://storage.googleapis.com/sourcegraph-assets/Docs/chat-with-cody-1124.png)
10447

105-
## 2. Suggest bug fixes and changes to code snippets
48+
## Code completions and suggestions
10649

107-
Cody is very efficient at catching bugs and suggesting improvements to code snippets. To try this out, let's run our previously generated unit test and see if Cody notices any issues. Inside your VS Code terminal, run the following command to try the unit test:
50+
Cody helps you code faster by providing real-time single and multi-line code completions. It uses the context of the code around your cursor to make accurate suggestions and starts predicting what you're trying to write before you type it.
10851

109-
```bash
110-
node tests/test.js
111-
```
52+
Let's try it by typing a `bubbleSort()` function in a JavaScript file.
11253

113-
This results in an error, as our function does not identify the `describe` statement.
54+
![cody-completions](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-completions-1124.png)
11455

115-
![Example of running failed unit test ](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-quickstart/unit-test-fail.png)
56+
Cody automatically predicts the function body in gray-dimmed text. Press `Tab` to accept the suggestion or `Esc` to dismiss it.
11657

117-
Let's paste this error into the Cody chat and see what suggestions it provides:
58+
## Use Cody to refactor code
11859

119-
![Example of error debugging with Cody ](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-quickstart/debug-with-cody.png)
60+
You can refactor your code with inline edits. All you need to do is highlight the code, hit the edit hotkey (`Opt + K`), and describe a change. Cody will generate a diff for the change in seconds.
12061

121-
Leveraging the failed test output, Cody is able to identify the potential bug and suggest a fix. It recommends installing `mocha`, importing it at the top of the `test.js` file to identify `describe`, and finally running it with `mocha`.
62+
Let's use the same `bubbleSort()` function from the previous section. Now, refactor the function to sort dates in descending order. Highlight the function and press `Opt + K`.
12263

123-
![Example of successfully running unit test ](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-quickstart/passed-tests.png)
64+
![cody-refactor](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-refactor-code-1124.png)
12465

125-
## 3. Ask Cody to add code documentation
66+
Type your refactoring suggestion in the input field and press `Enter`. Cody will generate a diff for the change in seconds. You can review the diff, accept the change, reject it, or retry if you are unsatisfied with the result.
12667

127-
Cody can also create well-crafted code documentation and comments, significantly improving the readability experience.
128-
You can add it by selecting a code snippet and running the **Document** command. Cody will add a comment to the top of the code snippet.
68+
## Use Cody to debug code
12969

130-
## Try other commands and Cody chat
70+
You can ask Cody to debug your code and fix bugs. Cody chat and inline edits are both quite powerful in debugging. If you are running into a bug, you can ask Cody to debug and fix the code. If you are a VS Code user and have the Cody extension installed, you can use **code actions** to debug your code.
13171

132-
That's it for this quickstart guide! Feel free to continue chatting with Cody to learn more about its [capabilities](/cody/capabilities). Cody can run some other useful commands including:
72+
When there is a mistake, a red warning is triggered. Along with this, you get a lightbulb icon. If you click on this lightbulb icon, there is an **Ask Cody to fix** option. Click this, and Cody will try to fix the bug with **Cody is working** notice.
73+
![code-actions](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-code-actions-vscode-1124.png)
13374

134-
- Explain code snippets
135-
- Refactor code and variable names
136-
- Translate code to different languages
137-
- Answer general questions about your code
138-
- Write context-aware code with a general awareness of the broader codebase
75+
That's it for this quickstart guide! Feel free to continue chatting with Cody to learn more about its [capabilities](/cody/capabilities).

0 commit comments

Comments
 (0)