madskristensen / NpmTaskRunner

Visual Studio extension
Other
88 stars 32 forks source link

Too much json removed on removing binding when package.json file not open #65

Open mrward opened 6 years ago

mrward commented 6 years ago

Installed product versions

Description

The package.json file can end up with invalid json after all the bindings are removed.

Steps to recreate

  1. Open a project with a package.json file.
  2. Ensure package.json file is not open in the text editor.
  3. Add a binding using the Task Runner Explorer. (Using the right click context menu - Bindings - checking one binding)
  4. Remove the binding using the Task Runner Explorer.

Current behavior

After step 3. the package.json is updated as expected. For example:

{
  "name": "nodejs-console-app1",
  "version": "0.0.0",
  "description": "NodejsConsoleApp1",
  "main": "app.js",
  "author": {
    "name": "matt"
  },
  "dependencies": {
    "gulp": "^3.9.1"
  },
  "devDependencies": {
    "gulp-cli": "^2.0.1"
  }
, "-vs-binding":{"BeforeBuild":["update"]}
}

After step 4 you end up with the last brace missing.

{
  "name": "nodejs-console-app1",
  "version": "0.0.0",
  "description": "NodejsConsoleApp1",
  "main": "app.js",
  "author": {
    "name": "matt"
  },
  "dependencies": {
    "gulp": "^3.9.1"
  },
  "devDependencies": {
    "gulp-cli": "^2.0.1"
  }

Expected behaviour

The package.json should be valid after the last binding is removed.

Ran into this whilst porting this extension to Visual Studio for Mac. I had to make two changes in the FileTextUtil, although I think the deletion behaviour is fixed by the second change.

Change 1) - The FileTextUtil includes the line endings in its ReadAllText method. It uses File.ReadAllText. I do not think this is correct since the rest of the code uses offsets based on the individual lines and ignores line endings. So I ended up the same code to what is in the VsTextViewTextUtil's ReadAllText method which uses the TryReadLine method and adds each line to a StringBuilder. This returns the text without any line endings.

Change 2) - it seems that if the ',' character preceding the binding section is the first character on the line the GetExtentInfo method returns the wrong information. So I changed the first while loop check to ensure that the next line is read when charCount matches the starting index. https://github.com/mrward/NpmTaskRunner/commit/0059b6e42ae7ab7f347ee86e69a3f24139c6876f