Cursor是一款专为编程而设计的带有人工智能功用的修改器,Cursor能够处理自然语言进行代码主动生成,协助程序员更快的编写代码,过错检测,并且能够创立更清晰和易于维护的代码等。

官网的介绍:

代码审查、编写、源码阅读,AI编辑器Cursor助你一臂之力!

接下来以日常开发中常见的运用场景与实例来看看的Cursor的强大功用吧!

一、运用场景与实例

只要两个快捷键:Ctrl+K是生成,Ctrl+L是chat

1、生成vue代码

描绘:用vue3和Element-plus生成登陆组件

生成过程如下:

代码审查、编写、源码阅读,AI编辑器Cursor助你一臂之力!

成果如下:

<template>
  <el-form>
    <el-form-item label="用户名">
      <el-input v-model="username"></el-input>
    </el-form-item>
    <el-form-item label="暗码">
      <el-input v-model="password" type="password"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="login">登录</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { ref } from 'vue';
import { ElForm, ElFormItem, ElInput, ElButton } from 'element-plus';
export default {
  components: {
    ElForm,
    ElFormItem,
    ElInput,
    ElButton,
  },
  setup() {
    const username = ref('');
    const password = ref('');
    const login = () => {
      // 登录逻辑
    };
    return {
      username,
      password,
      login,
    };
  },
};
<script>

2、剖析代码

能够协助咱们剖析别人写的代码和结构源码,实例如下:

代码审查、编写、源码阅读,AI编辑器Cursor助你一臂之力!

3、代码优化

能够协助咱们优化代码,给出更优主张,也能够作为代码检查的辅助工具。

代码审查、编写、源码阅读,AI编辑器Cursor助你一臂之力!

Cursor给的优化主张如下: 为了优化以上代码,咱们能够运用async/await语法来代替Promise的then/catch语法,使代码愈加简洁易读。一起,咱们能够运用解构赋值来简化代码。以下是优化后的代码:

async updPwd(data) {
  const regEmail =
    /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  const indentifyWay = regEmail.test(data.username) ? 'EMAIL' : 'PHONE'
  const { newpassword, newpassword2, username } = data
  const encryptPassword = await this.JSEncrypt(newpassword)
  const encryptConfirmPassword = await this.JSEncrypt(newpassword2)
  const params = {
    token: this.resetPwdToken,
    password: encryptPassword,
    confirmPassword: encryptConfirmPassword,
    indentifyWay,
    indentifyChannel: username
  }
  try {
    const { data } = await initPwd(params)
    this.state = 'setPwdSuc'
    this.$router.push('/login')
  } catch (error) {
    this.state = 'setPwdFail'
  } finally {
    this.$refs.llsLogin.loading = false
  }
}

优化解释:以上代码运用了async/await语法来代替Promise的then/catch语法,一起运用解构赋值来简化代码。在try块中,咱们运用解构赋值来获取initPwd函数返回的data特点,如果返回的code不是’200’,则设置状况为’setPwdFail’,不然设置状况为’setPwdSuc’,并跳转到登录页面。在catch块中,咱们设置状况为’setPwdFail’。在finally块中,咱们封闭loading状况。

4、修改代码

描绘:接口返回成果转成字符串并打印

代码审查、编写、源码阅读,AI编辑器Cursor助你一臂之力!

二、总结

Cursor功用现已很强大,能够协助咱们写简略重复的代码、偏逻辑的代码(比方正则表达式等)、优化代码、代码检查、代码提示…..

还有更多的运用场景等着咱们去发掘,毫无疑问AI现已成为咱们开发人员的好助手,随着AI练习它们将会给咱们开发和日常生活带来更多便利和惊喜,让咱们打开怀有开端拥抱AI吧!